jadupl
jadupl

Reputation: 210

Assembly fill array with Consecutive numbers

I have problem with simple array in Assembly. Is that correct approach. I need to fill up array with consectuive numbers, in this case it is 10. Code as follow:

section .data
array TIMES 10 db 0

section .text
global _start
_start:
mov eax,10  ;counter
mov ebx,0   ;start from value 0
mov ecx,array;

loop:   
mov [ecx],ebx
inc ecx     ;increase pointer to next array element
inc ebx     ;increase inputted value
dec eax     ;decrease iterator
jnz loop    ;if iterator not 0 loop

mov eax,1   ;eit
int 0x80

Is that correct code?

While debuging using gdb. Breakpoint on line 11 (5 loop) show following outputs:

$1: $eax = 4
$2: $ebx = 6
$3: $ecx = 134516902

x/d $ecx-1   0x80490a5:   5

How can i lookup for values from entire array? Only this place in me $ecx-1 shows correct value of inputed number. I am almost sure that I the issue is my code.

Kind Regards

Upvotes: 1

Views: 4573

Answers (2)

InfinitelyManic
InfinitelyManic

Reputation: 822

Sorry, I didn't realize until now that my last answered was not what you asked. Assumes byte size elements.

section .bss                                                                                                                                                            
        array:   resb 10                                                                                                                                                
section .data                                                                                                                                                           

section .text                                                                                                                                                           
        global _start                                                                                                                                                   
_start:                                                                                                                                                                 
        lea edi, [array]    ; pointer                                                                                                                                             
        mov ecx, 0   ; element counter                                                                                                                                                   
        mov eax, 1   ; starting number                                                                                                                                                   
loop:                                                                                                                                                                   
        mov [edi+ecx], eax   ; mov n to array element x                                                                                                                                           

        add ecx, 1                                                                                                                                                      
        add eax, 1                                                                                                                                                      
        cmp eax, 10                                                                                                                                                     
        jl loop                                                                                                                                                         

_exit:                                                                                                                                                                  
        mov eax,1   

output: (gdb) x/8b &array

0x80490a8 <array>:      1       2       3       4       5       6       0       0
21              cmp eax, 10
(gdb) 
0x80490a8 <array>:      1       2       3       4       5       6       0       0
22              jl loop
(gdb) 
0x80490a8 <array>:      1       2       3       4       5       6       0       0
17              mov [esi+ecx], eax
(gdb) 
0x80490a8 <array>:      1       2       3       4       5       6       7       0
19              add ecx, 1
(gdb) 
0x80490a8 <array>:      1       2       3       4       5       6       7       0
20              add eax, 1
(gdb) 
0x80490a8 <array>:      1       2       3       4       5       6       7       0
21              cmp eax, 10
(gdb) 
0x80490a8 <array>:      1       2       3       4       5       6       7       0
22              jl loop
(gdb) 
0x80490a8 <array>:      1       2       3       4       5       6       7       0
17              mov [esi+ecx], eax
(gdb) 
0x80490a8 <array>:      1       2       3       4       5       6       7       8

Upvotes: 1

InfinitelyManic
InfinitelyManic

Reputation: 822

There are several ways you can approach this. In the example below, we are using the lodsb (load string of size byte) instruction to retrieve values from an array of byte size elements. I'm not suggesting that this is the most efficient method.

  1 section .data
  2         array  db       3,1,4,1,5,9,2,6,5
  3         len.array equ   $-array
  4
  5 section .text
  6         global _start
  7 _start:
  8         lea esi, [array]  ; load array pointer
  9         mov ecx, len.array  ; use length of array as counter
 10 loop:
 11         lodsb        ; load string byte - will place loads in al;
 12         dec ecx      ; decrements ecx 
 13         cmp ecx, 0   ; 
 14         jnz loop
 15
 16 _exit:
 17
 18         mov eax,1   ;exit
 19         int 0x80

(gdb) display $ax ; you can see ax is being populated with array elements as you step through the code.

12              dec ecx
1: $ax = 3
(gdb)
13              cmp ecx, 0
1: $ax = 3
(gdb)
14              jnz loop
1: $ax = 3
(gdb)
11              lodsb
1: $ax = 3
(gdb)
12              dec ecx
1: $ax = 1
(gdb)
13              cmp ecx, 0
1: $ax = 1
(gdb)
14              jnz loop
1: $ax = 1
(gdb)
11              lodsb
1: $ax = 1
(gdb)
12              dec ecx
1: $ax = 4
(gdb)
13              cmp ecx, 0
1: $ax = 4
(gdb)
14              jnz loop
1: $ax = 4
(gdb)
11              lodsb
1: $ax = 4

Upvotes: 2

Related Questions