Sum of two integers
A program that calculates the sum of two given integer values. If the two values are equal, the tripled sum is returned.
Program source code
title .286C .model small .stack 100h data segment public fmtout db "Result: %d\n\0" data ends code segment public ; Start of the code segment assume ds:data, cs:code include Handyfar.inc ; Include the file with functions main proc ; Start of the main procedure call SetDataSegment ; Call the SetDataSegment procedure push 2 ; Save the value 2 on the stack push 3 ; Save the value 3 on the stack call Calculate ; Call the Calculate procedure (return value in ax) push ax ; 2nd: save the ax register value on the stack lea ax, ds:[fmtout] ; 1st: load the address of the fmtout format string into ax call PrintFormatString ; Call the PrintFormatString procedure add sp, 2 ; Align the stack push 2 ; Save the value 2 on the stack push 2 ; Save the value 2 on the stack call Calculate ; Call the Calculate procedure (return value in ax) push ax ; 2nd: save the ax register value on the stack lea ax, ds:[fmtout] ; 1st: load the address of the fmtout format string into ax call PrintFormatString ; Call the PrintFormatString procedure add sp, 2 ; Align the stack xor al, al ; Clear the al register call ExitProcess ; Call the ExitProcess procedure main endp ; End of the main procedure Calculate proc ; Start of the Calculate procedure push bp ; Save the bp register value on the stack mov bp, sp ; Copy the sp register value into bp pushf ; Save the FLAGS register value on the stack push bx ; Save the bx register value on the stack mov ax, word ptr ss:[bp+4] ; Copy the value of the first argument at the stack address [bp+4] cmp ax, word ptr ss:[bp+6] ; Compare ax with the value of the second argument at the stack address [bp+6] jz @Triple ; If ZF=1, jump to the @Triple label add ax, word ptr ss:[bp+6] ; Add the value at the stack address [bp+6] to the ax register jmp @Done ; Jump unconditionally to the @Done label @Triple: add ax, word ptr ss:[bp+6] ; Add the value at the stack address [bp+6] to the ax register mov bx, 3 ; Copy the number 3 into the bx register imul bx ; Multiply the ax register value by the bx value @Done: pop bx ; Pop the value from the stack into bx popf ; Pop the flags from the stack into the FLAGS register pop bp ; Pop the value from the stack into bp ret 4 ; Return from the procedure with the stack aligned by 4 bytes Calculate endp ; End of the Calculate procedure code ends ; End of the code segment end main ; End of file. The main procedure is the entry point
Result: 5
Result: 12
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".