0
votes

My problem is not too big but I don't understand something and I wanted to ask for some help.

We are learning about Assembly .x86 language and I got stuck at SUB arithmetic Instruction.

This is the code:

INCLUDE Irvine32.inc

.data

val_A    DWORD   1234h
val_B    DWORD   144h

.code

main proc

MOV     EAX, val_B   ;This works
SUB     EAX, val_A

CALL    WriteInt
CALL    Crlf

INVOKE  ExitProcess, 0
main endp

.stack
dw      100 dup(? )

end main

When I try to put like this:

MOV    EAX, val_A
MOV    EBX, val_B
SUB    EBX, EAX

It doesnt work. We learned that [SUB] arithmetical intstruction works just as [MOV] and [ADD] but when I try to use it as [SUB reg/reg] absolutely gives nothing only shows what is in the EAX register. I use Visual Studio I am not sure if that's the issue.

1
The result is stored in EBX. So you'd need to add MOV EAX,EBX to keep WriteInt happy and have it display the correct value.Hans Passant
Ohh.... This is simple... Thank you :)Terminatorka
First version puts result in EAX, second in EBX. So since your WriteInt expects (I suppose) input to be in EAX you got different bahavior.Artur
The WriteInt is hard-coded to display value of eax, it can't read your mind to switch to ebx instead. Overall almost no asm instruction does care about what was executed before, all the "state" of CPU is basically in the content of registers, and the current content of memory, it has little idea how it got into particular state (for example the CPU does not know it is "inside" subroutine, and how deep). You can use built-in debugger in Visual Studio to check content of registers and memory after each instruction execution (single stepping the code), which I strongly recommend.Ped7g
I thought it works just as CBW, etc. So everything goes linear but it doesnt. It makes sense now. Thank you! :)Terminatorka

1 Answers

0
votes

Sub instruction does work like MOV and ADD. It does work as SUB reg/reg. When you tried to use SUB as:

MOV EAX, val_A
MOV EBX, val_B
SUB EBX, EAX

You didn't change EAX, and like you said, the register EAX hasn't changed. That because the result saved in EBX. To get the result of (1234h - 123h) into the register EAX you should code:

MOV EAX, val_A
MOV EBX, val_B
SUB EAX, EBX

Just to be clear again, SUB instruction does work like MOV and that why the result moves into the first register. For example:

MOV EAX, 1234h

put the number 1234h in the register EAX and not the opposite.