Functions that perform addition and subtraction
In the following program example we will write two functions that perform addition and subtraction. The results will be saved in variables.
Program source code
; Author: Evgeny Sergeevich Dovgopolov add_res: db 00h sub_res: db 00h main: mvi b, 05h ; Copy the hexadecimal value 05h into register B mvi c, 03h ; Copy the hexadecimal value 03h into register C mvi d, 02h ; Copy the hexadecimal value 02h into register D call add8b_v ; Call the procedure add8b_v. The result is in A sta add_res ; Save the accumulator value in the variable add_res mvi b, 10h ; Copy the hexadecimal value 10h into register B mvi c, 04h ; Copy the hexadecimal value 04h into register C mvi d, 03h ; Copy the hexadecimal value 03h into register D call sub8b_v ; Call the procedure sub8b_v. The result is in A sta sub_res ; Save the accumulator value in the variable sub_res hlt ; Stop program execution add8b_v: mov a, b ; Copy the value of register B into register A add c ; Add the value of register C to register A add d ; Add the value of register D to register A ret ; Return from the procedure back to the call site sub8b_v: mov a, b ; Copy the value of register B into register A sub c ; Subtract the value of register C from register A sub d ; Subtract the value of register D from register A ret ; Return from the procedure back to the call site
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".