Search code examples
assemblyx86-16tasm

Read two inputs with multi digits in assembler


I am learning Assembly using TASM, for University and I am a complete beginner.

I have an assessment in which I need to get two inputs (example: 12, 20) and decide which one is bigger. Currently I managed to read one multiple digit input and store it into BL but I don't know how to read another one.

How can I output which input is greater?

Thanks in advance!

My code:

.model small
.stack 100h
.data

num db 0

.code
start:
        mov ax, @data
        mov ds, ax 

        mov dl, 10  
        mov bl, 0         

scanNum:

        mov ah, 01h
        int 21h

        cmp al, 13   ; Check if user pressed ENTER KEY
        je  exit 

        mov ah, 0  
        sub al, 48   ; ASCII to DECIMAL

        mov cl, al
        mov al, bl   ; Store the previous value in AL

        mul dl       ; multiply the previous value with 10

        add al, cl   ; previous value + new value ( after previous value is multiplyed with 10 )
        mov bl, al

        jmp scanNum    

exit:

       mov ah, 04ch   
       int 21h

end start

Solution

  • Make scanNum into a function, by putting ret at the end. Then you can execute it twice. After the first call to scanNum, save the return value in a place that is not used by the scanNum function.

    Once you have read both numbers, use cmp to compare them.

    Something like this:

        call scanNum
        mov bh, bl
        call scanNum
    
        <compare bl and bh to choose which one to print>
    
    exit:
        mov ah, 04ch   
        int 21h
    
    scanNum:
        mov dl, 10  
        mov bl, 0         
    
    scanNumLoop:
        <same as before, except jmp to scanNumExit when done>
        jmp scanNumLoop    
    
    scanNumExit:
        ret