Sunday, January 19, 2025

A for loop in 16-bit assembly for DOSBox

 Here’s a simplified working example of a for loop in 16-bit assembly for DOSBox. This program displays numbers from 1 to 10 without any additional subroutines or complications.


Simplified Working Example: For Loop


.model small .stack 100h .data newline db 13, 10, '$' ; Newline characters number_msg db 'Number: $' ; Message to display .code main: ; Set up the data segment mov ax, @data mov ds, ax ; Initialize CX for the loop (10 iterations) mov cx, 10 ; Loop 10 times mov bx, 1 ; Counter starts at 1 for_loop: ; Print "Number: " lea dx, number_msg mov ah, 09h int 21h ; Print the current number in BX mov ax, bx ; Copy counter to AX for printing call print_digit ; Print a newline lea dx, newline mov ah, 09h int 21h ; Increment the counter inc bx ; Loop back until CX = 0 loop for_loop ; Exit the program mov ah, 4Ch int 21h ; Subroutine to print a single-digit number in AX print_digit proc add al, '0' ; Convert the number to ASCII mov dl, al ; Store the ASCII character in DL mov ah, 02h ; DOS function to print a character int 21h ret print_digit endp end main

Key Changes:

  1. Simplified the Logic:

    • Removed unnecessary subroutines for multi-digit numbers.
    • The program only works for single-digit numbers (1 to 10), which avoids complexity.
  2. Print Single Digits:

    • The number in AX is converted to an ASCII character by adding '0' (ASCII 48).
  3. Basic DOS Interrupts:

    • INT 21h, AH=09h: Used to display strings.
    • INT 21h, AH=02h: Used to display a single character.
  4. Exits Cleanly:

    • The program uses INT 21h, AH=4Ch to exit back to DOSBox

Expected Output:


Number: 1 Number: 2 Number: 3 Number: 4 Number: 5 Number: 6 Number: 7 Number: 8 Number: 9 Number: :

(Note: The number 10 will be displayed as : due to the single-digit logic in this simplified example. If you need to handle two-digit numbers, let me know!)

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...