Search code examples
assemblyx86

Can somebody help me out with this expression written in assembly using arithmetic operations?


The expression: EAX = 7*EAX - 2*EBX - EBX/8
My code:

start:
    mov EAX,9
    mov EDX ,7
    mul EDX ; EAX<-EAX*EDX
    mov EDX,EAX ; EDX<-EAX*EDX<=>(7*9)
    mov EBX,8
    mov EAX,2
    mul EBX; EAX<- EAX*EBX (2*8)
    mov ECX,EAX
    sub EDX,ECX; <=>7*EAX-2*EBX (7*9-2*8)
    mov EAX,EBX
    div EBX ; EAX <=> 8/8
    sub EDX,EAX; <=> 7*9-2*8-8/8
    push 0
    call exit
end start

Solution

  • EAX = 7*EAX - 2*EBX - EBX/8
    

    It is not because an expression contains multiplication (*) or division (/), that your solution should be using these operations.

    • For 7*EAX you can use the simpler imul eax, 7.
    • Instead of doubling EBX, you could simply subtract EBX twice.
    • And for calculating an eighth of EBX, you should simply shift its value 3 times to the right.
    imul eax, 7
    sub  eax, ebx
    sub  eax, ebx
    shr  ebx, 3
    sub  eax, ebx