Three-digit numbers made of 1, 2, 3, 4
A program that generates and prints all possible three-digit numbers made of the digits 1, 2, 3, 4 without repetitions, using nested loops.
Program source code
title .model small .stack 100h data segment public string db "Three-digit numbers:\n\0" result db "\nTotal number of three-digit numbers: %d\0" fmtstr db "%d%d%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 PrintFormatString ; Call the PrintFormatString procedure mov ax, 1 ; Set the ax counter to 1 OuterLoop: mov bx, 1 ; Set the bx counter to 1 InnerLoop: mov cx, 1 ; Set the cx counter to 1 InsideLoop: cmp cx, ax ; Check the cx index counter value against the ax index counter jz Skip ; If ZF=1, jump to the Skip label cmp cx, bx ; Check the cx index counter value against the bx index counter jz Skip ; If ZF=1, jump to the Skip label cmp ax, bx ; Check the ax index counter value against the bx index counter jz Skip ; If ZF=1, jump to the Skip label inc numsum ; Increment the numsum variable value by 1 push ax ; Temporarily save the ax register value on the stack push cx ; 4th: save the cx register value on the stack push bx ; 3rd: save the bx register value on the stack push ax ; 2nd: save the ax register value on the stack lea ax, ds:[fmtstr] ; 1st: load the address of the fmtstr format string into the ax register call PrintFormatString ; Call the PrintFormatString procedure add sp, 6 ; Align the stack pop ax ; Pop the temporarily saved value from the stack into the ax register Skip: inc cx ; Increment the cx counter by 1 cmp cx, 4 ; Compare the cx register value with the number 4 jbe InsideLoop ; If CX is less than or equal to 4, jump to InsideLoop inc bx ; Increment the bx counter by 1 cmp bx, 4 ; Compare the bx register value with the number 4 jbe InnerLoop ; If BX is less than or equal to 4, jump to InnerLoop inc ax ; Increment the ax counter by 1 cmp ax, 4 ; Compare the ax register value with the number 4 jbe OuterLoop ; If AX is less than or equal to 4, jump to OuterLoop push numsum ; 2nd: save the numsum variable value on the stack lea ax, ds:[result] ; 1st: load the address of the result variable into the ax register 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
Three-digit numbers:
123 124 132 134 142 143 213 214 231 234 241 243 312 314 321 324 341 342 412 413 421 423 431 432
Total number of three-digit numbers: 24
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".