Skip to main content

The Additon & Subtraction in Assembly

There is not much difficulty when it comes to addition and subtraction in assembly programming.

Simply, additon and substraction breaks down to the following:


    add eax, ecx                    ; eax = eax + ecx, result in eax
    add eax, DWORD [ebp-4]             ; eax = eax + localVar1, result in eax
    add DWORD [ebp-4],DWORD [ebp -4]     ; illegal, with all instruction both operands can never be memory 
    add DWORD [ebp-4], eax            ; [ebp-4] = [ebp-4] + eax

    sub eax, ecx                    ; eax = eax - ecx, result in eax
    sub eax, DWORD [ebp-4]             ; eax = eax - localVar1, result in eax
    sub DWORD [ebp-4],DWORD [ebp -4]     ; illegal, with all instruction both operands can never be memory 
    sub DWORD [ebp-4], eax            ; [ebp-4] = [ebp-4] - eax
A simple program to display the message about an arithetic operation like "Math: 8 + 4 = ?" can be achived by the following code block:
;an equivalent program to this in assembly
SECTION .data

operChar: db '+',0
msg: db 'Math: %d %c %d = %d',10,0

SECTION .text
;allow access to printf
extern printf
;make our main available externally
global main

main:    ;int main(int numArguments, char* arg[])
     push ebp
     mov ebp , esp
    sub esp, 4    ;reserve space for a 32 bit variable[4 byes= 8*4=32]

    ;set up the register what will hold the values we want to operate on
    mov eax , 8
    mov edx , 4
    
    push eax    ;save value of eax; so msg can be displayed correctly
    add eax, edx ;translates to eax = eax + edx
    mov ecx, eax    ;mov result into ecx
    pop eax     ;restore value of eax 

    ;recall that printf tooks like
    ;printf(msg,eax,operChar,edx,result)
    push ecx    ;temporary- we will get the value using assembly, for now just bare with me
    push edx
    push DWORD [operChar]
    push eax
    push msg
    call printf
    add esp, 20     ;this cleans up the stack; we pushed 5 things unto the stack each of 4 bytes long = 5*4


     mov esp, ebp
     pop ebp
     ret

Comments

  1. Thank you for this. You've really helped me. Your videos are great.

    ReplyDelete

Post a Comment