Search code examples
debuggingassemblyx86-16dosbox

Solving a problem with comparing numbers in DEBUG.EXE and outputting the largest number


There was a problem with understanding and how to solve the problem with DEBUG.EXE.

Assignment:

In the DEBUG debugger, write a program that allows you to enter single-digit numbers from the keyboard, compare them and output the largest of them. Save the program to disk.

Studying the documentation and commands that could be used to solve the problem and write the program code, it was not possible to come up with even working code. One of the previous attempts to launch the program in DEBUG.EXE via DOSBox ended in a crash.
How should the code be written so that the program will work, and even be capable of the above operations in the task when it is launched?

I used the following code as one of the attempts:

n prog.com
a 100
mov ah, 01
int 21
mov al, dl
mov ah, 01
int 21
mov bl, dl
cmp al, bl
jae 116
mov dl, al
int 21
jmp 11a
mov dl, bl
int 21
ret

Solution

    1. Create a new file under control of the Operating System which DEBUG can write data to

      -n d:\prog.com

    2. Machine executable code in memory starts at CS:0100

      -a 100

    Offset  Opcodes     Instruction     Description
    
    0100    b4 01       mov ah,1        ; 1st digit, character input with echo      
    0102    cd 21       int 21h
    
    0104    88 c2       mov dl,al       ; save result in dl
    
    0106    b4 01       mov ah,1        ; 2nd digit, character input with echo
    0108    cd 21       int 21h
    
    010a    88 c6       mov dh,al       ; save result in dh
    
    010c    38 f2       cmp dl,dh       ; compare digits
    010e    77 0a       ja 0116         ; if greater
    0110    72 04       jb 0114         ; if smaller    
    
    0112    b6 3d       mov dh,3dh      ; digits are equal, print '='
                        
    0114    88 f2       mov dl,dh        
    
    0116    b4 02       mov ah,2        ; print out digit
    0118    cd 21       int 21h
    
    011a    b8 00 4c    mov ax,4c00h    ; quit to dos
    011d    cd 21       int 21h
    
    1. Save to file 1fh (31) bytes
    -rcx
    CX 0000
    :1f
    
    1. Save & quit
    -w
    Writing 0001f bytes
    -q