Question: The code below works, but I wanted to know why it works, since to my knowledge, this code: mov [ array + eax ] ,

The code below works, but I wanted to know why it works, since to my knowledge, this code:
mov [array + eax], ebx
mov ecx, [array + eax]
found under loop_start, should need to be:
mov [array + eax *4], ebx
mov ecx, [array + eax *4]
And in other code I have written, I have had to do that, with the lack of a "*4" making the code not work. In this case, however, the opposite is true, and I have to remove the "*4" for the code to give the intended output. If I leave the "*4", the loop stops counting at 10, even though it is meant to count to 20. This is odd because the code above doesn't even touch the counter variable. The full code is below, please advise.
extern printf ; the C function, to be called
global main ; the standard gcc entry point
section .bss ; BSS, uninitialized identifiers
array resw 20
count resd 1
value resd 1
section .data ; Data section, initialized identifiers
fmt db "Array index: %d Value: %d",10,0
section .rodata ; Read-only section, immutable identifiers
section .text ; Code section.
main: ; the program label for the entry point
; Don't change or remove the lines of code in here |
push ebp ; set up stack frame |
mov ebp, esp ; |
; Don't change or remove the lines of code in here |
;
; Your NASM code will go in here
;
; Move number into counter and value
mov dword [count],0
mov dword [value],2
loop_start:
; Compare count to 20, greater than or equal then end loop
cmp dword [count],20
jge loop_end
; Move into array
mov eax, [count]
mov ebx, [value]
mov [array + eax], ebx
mov ecx, [array + eax]
; Print output
push dword ecx
push dword [count]
push fmt
call printf
add esp, 12
; Increment the counter
add dword [count],1
add dword [value],2
; Restart loop
jmp loop_start
loop_end:
; Don't change or remove the lines of code in here |
mov esp, ebp ; takedown stack frame |
pop ebp ; |
; |
mov eax, 0 ; no error return value |
ret ; return |
; Don't change or remove the lines of code in here |

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!