Numbers from 100 to 200 divisible by 9
A program that finds all numbers in the range from 100 to 200 that are divisible by 9 and computes their sum.
Program source code
title .model small .stack 100h data segment public string db "Numbers from 100 to 200 divisible by 9:\0" result db "\nSum: %d\0" fmtstr db "\n[%d]\0" numsum dw ? 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 lea ax, ds:[string] ; Load the address of the string variable into the ax register call WriteString ; Call the WriteString procedure mov cx, 101 ; Set the counter to 101 mov bx, 9 ; Copy the value 9 into the bx register MainLoop: mov ax, cx ; Copy the value of the cx counter into the ax register cwd ; Sign-extend the dx register. The dx register is cleared div bx ; Divide the value in ax by the value in bx test dl, dl ; Check whether the value in the dl register is 0 jnz Skip ; If ZF=0, jump to the Skip label push cx ; 2nd: save the value of the cx counter register on the stack lea ax, ds:[fmtstr] ; 1st: load the address of the fmtstr format string into ax call PrintFormatString ; Call the PrintFormatString procedure add sp, 2 ; Align the stack add numsum, cx ; Add the value of the cx counter to the numsum variable Skip: inc cx ; Increment the cx counter register by 1 cmp cx, 200 ; Compare the value of the cx counter register with the number 200 jbe MainLoop ; If CX is less than or equal to 200, jump to MainLoop push numsum ; 2nd: save the value of the numsum variable on the stack lea ax, ds:[result] ; 1st: load the address of the result variable 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 code ends ; End of the code segment end main ; End of file. The main procedure is the entry point
Numbers from 100 to 200 divisible by 9:
[108]
[117]
[126]
[135]
[144]
[153]
[162]
[171]
[180]
[189]
[198]
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".