Difference from 51
A program that computes the absolute difference between a number and 51. If the number is greater than 51, the result is tripled.
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 30 ; Save the value 30 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] mov bx, 51 ; Copy the number 51 into the bx register cmp ax, bx ; Compare the value in the ax register with the value in bx ja @Triple ; If CF=0 and ZF=0, then jump to the label @Triple cmp ax, bx ; Compare the value in the ax register with the value in bx jz @Zero ; If ZF=1, then jump to the label @Zero sub bx, ax ; Subtract the value in the ax register from the bx register xchg ax, bx ; Exchange the contents of ax with the contents of the bx register jmp @Done ; Jump unconditionally to the @Done label @Triple: sub ax, bx ; Subtract the value in the bx register from the ax register mov bx, 3 ; Copy the number 3 into the bx register imul bx ; Multiply the value in the ax register by the value in bx jmp @Done @Zero: xor ax, ax ; Clear the value of the ax register @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 2 ; Return from the procedure with the stack aligned by 2 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: 21
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".