tags:

views:

88

answers:

2

I have the following piece of code, which should at the breakpoint show 123 at eax and 321 at ecx. For some reason that is not happening. Anyone cares to explain why?

 push ebp;
 mov ebp, esp;
 sub esp, 8;
 mov [ebp-4], 123;
 mov [ebp-8], 321;
 mov eax, [ebp-4];
 mov ecx, [ebp-8];
 pop ebp; <------------- breakpoint here
 retn;

I guess what must be wrong is that I cannot do

mov [ebp-4], 123

?

Everything else seems fine to me.

Thanks

edit: The values are: eax 1505915; ecx 1720129;

+7  A: 

You're storing byte values into memory.

Change to

mov dword ptr [ebp - 4], 123
mov dword ptr [ebp - 8], 321

eax = 1505915 is 0x16FA7B. The last byte is 7B in hex, which is 123 in decimal. ecx = 1720129 is 0x1A3F41. 41 in hex is the last byte of 321 (141).

Michael
And Michael saves the day for the 999th time. Thanks!
devoured elysium
Great answer :)
OJ
Another question. If I want to do inc [ebp-4], will I have to do inc dword ptr [ebp-4]? I'd say that if the number is small it doesn't matter, but if the number grows bigger then it might cause problems. What byte size does assembly assume by default?
devoured elysium
Everytime there could be an ambiguity, should I use dword ptr? This is, everytime I use [base + addr] ?
devoured elysium
It depends on your assembler, and you should always just specify it to be certain. There are byte, word, and dword versions of inc.
Michael
Thanks!
devoured elysium
+1  A: 

I was under the impression (and I might be wrong) that you can't push values into memory addresses like that. You have to go via registers:

MOV EAX, 123
MOV [EBP-4], EAX

As I said, I might be wrong. But give it a go. Check out the instruction set manual as well. It'll show you what operations you can do on what addressing modes.

OJ
This answer is a fail :)
OJ
It's x86 - nearly anything goes, storing immediates directly into memory (internally, modern x86 CPU's have a load/store architecture and microcode similar to what you have is what would actually execute.)
Michael
Thanks Michael. I need to get back into ASM. Time to break out the instruction manual. I appreciate the feedback :)
OJ