1
votes

I am taking assembly language class and have a project but I'm somewhat stuck and can't find what is wrong in my code. The project asks me to write a couple basic arithmetic operation (addition and subtract) based on the 2 values that are divided from my student ID.

My student ID has 7 digits and the 3 most significant digits are stored in variable "left" that is moved to register EAX, the 4 least significant digits are stored in variable "right" that is moved to register EBX. I'm asked to find the total and positive difference from the 2 values above and then store the total value and positive difference value to 2 different registers (ECX and EDX respectively).

I already found the total using ADD instruction just find. However, when I do the positive difference using SUB instruction, all I get is the "left" value or the negative difference. Below is the code:

mov eax,left        ;// get first 3 digits
call DumpRegs       ;// DumpReg to display the contents of the register
mov ebx,right       ;// get last 4 digits
call DumpRegs
add eax, ebx        ;// add both values together
call DumpRegs
mov total,eax
call DumpRegs
mov ecx,total
call DumpRegs
mov eax,left
call DumpRegs
call WriteInt

mov eax,left
call DumpRegs
call WriteInt
mov ebx,right
call DumpRegs         
sub ebx,eax
mov diff,ebx
mov edx,diff
mov ebx,right
call DumpRegs

For example, if my student ID is 1234567, then left is 123 and 4567 is right. So eax will be 123 and ebx will be 4567. The total of eax and ebx will be a number stored to "total" which is registered to ecx. Then the positive difference I got according to my code is 123. If I switch

sub ebx,eax

to

sub eax,ebx

I then get a negative value. I can't figure out why the sub only show the left value.

Edit: this is the content of the registers after each instruction

  EAX=000000F2  EBX=7F0EF000  ECX=012A1055  EDX=012A1055
  ESI=012A1055  EDI=012A1055  EBP=006BFC54  ESP=006BFC44
  EIP=012A352A  EFL=00000246  CF=0  SF=0  ZF=1  OF=0  AF=0  PF=1


  EAX=000000F2  EBX=00001860  ECX=012A1055  EDX=012A1055
  ESI=012A1055  EDI=012A1055  EBP=006BFC54  ESP=006BFC44
  EIP=012A3535  EFL=00000246  CF=0  SF=0  ZF=1  OF=0  AF=0  PF=1


  EAX=00001952  EBX=00001860  ECX=012A1055  EDX=012A1055
  ESI=012A1055  EDI=012A1055  EBP=006BFC54  ESP=006BFC44
  EIP=012A353C  EFL=00000202  CF=0  SF=0  ZF=0  OF=0  AF=0  PF=0


  EAX=00001952  EBX=00001860  ECX=012A1055  EDX=012A1055
  ESI=012A1055  EDI=012A1055  EBP=006BFC54  ESP=006BFC44
  EIP=012A3546  EFL=00000202  CF=0  SF=0  ZF=0  OF=0  AF=0  PF=0

+6482
  EAX=00001952  EBX=00001860  ECX=00001952  EDX=012A1055
  ESI=012A1055  EDI=012A1055  EBP=006BFC54  ESP=006BFC44
  EIP=012A3556  EFL=00000202  CF=0  SF=0  ZF=0  OF=0  AF=0  PF=0


  EAX=000000F2  EBX=00001860  ECX=00001952  EDX=012A1055
  ESI=012A1055  EDI=012A1055  EBP=006BFC54  ESP=006BFC44
  EIP=012A3560  EFL=00000202  CF=0  SF=0  ZF=0  OF=0  AF=0  PF=0

+242
  EAX=000000F2  EBX=00001860  ECX=00001952  EDX=012A1055
  ESI=012A1055  EDI=012A1055  EBP=006BFC54  ESP=006BFC44
  EIP=012A356F  EFL=00000202  CF=0  SF=0  ZF=0  OF=0  AF=0  PF=0

+242
  EAX=000000F2  EBX=00001860  ECX=00001952  EDX=012A1055
  ESI=012A1055  EDI=012A1055  EBP=006BFC54  ESP=006BFC44
  EIP=012A357F  EFL=00000202  CF=0  SF=0  ZF=0  OF=0  AF=0  PF=0

+242
  EAX=000000F2  EBX=00001860  ECX=00001952  EDX=0000176E
  ESI=012A1055  EDI=012A1055  EBP=006BFC54  ESP=006BFC44
  EIP=012A359D  EFL=00000202  CF=0  SF=0  ZF=0  OF=0  AF=0  PF=0

Hello world!☺Press any key to continue . . .
1
Looks correct to me, what's the issue? Notice that in the last dump the diff is in edx, and that seems to be 0x1860 (ebx, right) - 0xf2 (eax, left). Also ecx has the correct total.Jester
the positive subtraction's the issue. It should have given 5998 (my ID is 2426240) but it doesn't. Instead it gave out the left value (242) even though I subtract the left(242) value from the right(6240)Nathan
You never print it, it's only in the dump. edx does have 0x176E which is 5998. There is no WriteInt call for the diff anywhere. The last WriteInt call in your code as shown is before the subtraction and will just print left.Jester
I just tried and it still show the left value. It conflicts with the value registered in EDX (I noticed that EDX does have the value)Nathan
No idea what you mean. The code as shown does only print left, never diff.Jester

1 Answers

1
votes

Running this code:

mov left, 123
mov right, 4567

mov eax,left
call DumpRegs
call WriteInt
mov ebx,right
call DumpRegs         
sub ebx,eax
mov diff,ebx

Correctly produces the positive difference 4444. If it doesn't for you, please edit your question so it includes the output of all those DumpRegs calls.