x86 assembly registers addresses

2.1k views Asked by At

I tried to do it myself but I couldn't manage with it properly. Below is my exam question which I'd like to do properly and understand how it works. I would be grateful if you could help me with it.

Determine the destination (register or memory address) and the value stored by each instruction of the following program fragment:

mov eax, 0x8000
mov ebx, 0x40000
lea esp, [ebx]
shl eax, 16
sar ebx, 23
lea ecx, [ebx+0xff]
push    ecx
sar eax, 31
push    eax
mov eax, [esp+4]
sub eax, [esp]
2

There are 2 answers

0
Marcus Müller On

This is the intel assembler syntax, so each line is always

instruction destination source

eax, esp etc are all register names.

Numbers are interpreted as number constants.

[ expression ]

can be used to calculate an address and then load the value from that address.

I'm pretty optimistic you could have figured this out (and learned much more) by reading the obvious wikipedia page, which even links to a whole wikibook on x86 assembler.

1
Nayuki On

Annotating your code:

mov eax, 0x8000      ; Move the 32-bit value 0x8000 into register eax
mov ebx, 0x40000     ; Move the 32-bit value 0x40000 into register ebx
lea esp, [ebx]       ; Load the value of register ebx into register esp
shl eax, 16          ; Take the value of register eax, shift left 16 bits, and store back into eax
sar ebx, 23          ; Take the value of register ebx, shift right 23 bits (copying sign bit), and store back into ebx
lea ecx, [ebx+0xff]  ; Load the value of (register ebx) + 0xFF into register ecx
push    ecx          ; Push the value of register ecx onto the stack (the memory near the address of the value of register esp)
sar eax, 31          ; Take the value of register ebx, shift right 31 bits (copying sign bit), and store back into ebx
push    eax          ; Push the value of register eax onto the stack
mov eax, [esp+4]     ; Move the vaule of register the memory at address (esp + 4) and store into eax
sub eax, [esp]       ; Subtract the value of the memory at address esp from eax and store into eax