Reputation: 21
We have been assigned to perform an array-like code in Assembly Language. We're using Intel x086 architecture/system/code.
The code we created is supposed to store 3, 4, 5 into 3 different memory locations, we called as [i+EAX]. EAX defines memory allocations.
The problem with this, is when we retrieve the values stored inside the memory locations pointed out by [i+EAX], the resulting values are garbage.
Where are we wrong? 1. Did we add EAX incorrectly? Supposing +1, +4, +8, to signify storing of the next integer to the next heap, we still yielded wrong answers. It was still garbage. 2. Is the memory location number in hexa or deci? We tried both, but memory tracing proves that our output is still garbage.
Here's the code:
global _main
extern _system, _printf
section .text
_main:
; clear screen
push clr
call _system
add esp, 4
MOV EAX, 0001
MOV EBX, 0003
;FIRST - Initialize to 3.
MOV dword [i+EAX], EBX
push dword [i+EAX]
push prompt
call _printf
add esp, 8
ADD EAX, 0008 ; Assuming next memory space will be allocated at 0009
INC EBX ; add value, to increase to 4.
;SECOND - Initialize to 4.
MOV dword [i+EAX], EBX
push dword [i+EAX]
push prompt
call _printf
add esp, 8
ADD EAX, 0008
INC EBX;
;THIRD - Initialize to 5.
MOV dword [i+EAX], EBX
push dword [i+EAX]
push prompt
call _printf
add esp, 8
;RETRIEVE FIRST - Which should be "3"
MOV EAX, 0001
push dword [i+EAX]
push prompt
call _printf
add esp, 8
; RETRIEVE SECOND - Which should be "4", but shows garbage value. Why?
MOV EAX, 0002
push dword [i+EAX]
push prompt2
call _printf
add esp, 8
ret
section .data
clr db "cls",0
prompt db "Value is %d",13,10,0
prompt2 db "EAX testing X is %x",13,10,0
prompt3 db "EAX testing D is %d",13,10,0
i dd 0
Please help us. Thank you!
Upvotes: 0
Views: 461
Reputation: 31203
You didn't say on which OS/calling convention you are using, so I have to do some guessing.
First, why do you set EAX
to 1 for the first value? You'll just ensure an unaligned access and are skipping one byte of memory. Set it to zero.
Second, you don't store the value of EAX
when calling _printf
. Most likely _printf
is storing its return value in EAX
, so next time when you add 8 to EAX
it will not be 9, but it will be something completely different.
Even if _printf
isn't storing its return value in EAX
, you are not using the next dword, which would be 4. See also next point.
Third, when printing values out you suddenly use values 1 and 2 for EAX
to access the value i
, which of course won't give you two dwords located one after another. You will access completely different memory area. You have to use the same values to save and retrieve values.
So even if EAX
wasn't modified, you are storing something in bytes i+1 - i+4, then i+9 - i+12 and then i+13 - i+16. Then when printing you are accessing bytes i+1 - i+4 and i+2 - i+5. As you can see, you never write to i+5 so it will contain garbage.
Set EAX
to 0, 4, 8 etc explicitly. Don't add anything. Then see what is stored and what is output. Or push EAX also when calling and pop afterwards if you really need to use add (here you don't).
Upvotes: 3