Search code examples
emu8086

how to display the smallest value in my code in emu8086?


I had made a code that display the largest but then my teacher ask us to make another one that input 3 numbers and display the smallest value.

here is the code:

org 100h

    jmp start

    msg1 db 10,13,"Enter first number: $"
    msg2 db 10,13,"Enter second number: $"
    msg3 db 10,13,"Enter third Number: $"

    num1 db ?
    num2 db ?
    num3 db ?

start:

    lea dx, msg1
    mov ah, 9
    int 21h
    mov ah, 1
    int 21h
    mov num1, al 
    lea dx, msg2
    mov ah, 9
    int 21h
    mov ah, 1 
    int 21h
    mov num2, al
    lea dx, msg3
    mov ah, 9
    int 21h
    mov ah, 1
    int 21h
    mov num3, al

    mov bl, num1
    cmp bl, num2
    jng number2 

    cmp bl, num3
    jng number3

    mov ah, 2
    mov dl, num1
    int 21h
    jmp escape 

number2:

    mov bl, num2
    cmp bl, num3
    jng number3

    mov ah, 2
    mov dl, num2
    jmp escape

number3:

    mov ah, 2
    mov dl, num3
    int 21h

escape:
    ret

sample output:

1st no. i enter 3

2nd no, i enter 2

3rd no, i enter 1

and the largest is 3 but the output will be 13 because i don't know how to put space on my code :D...

Pls help!!! XD Also it's my first time posting this... so sorry for my bad grammar ty.


Solution

  • mov ah, 2
    mov dl, num2
    jmp escape
    

    In this part your program forgot to actually call DOS with int 21h.

    i don't know how to put space on my code

    Just use the following everywhere you need some space between outputs on the same line:

    mov ah, 2
    mov dl, " "
    int 21h
    

    Or put items on different lines using:

    mov ah, 2
    mov dl, 10
    int 21h
    mov dl, 13
    int 21h
    

    A nicer solution would be to display a suitable message before outputting the number:

    msg4 db 10,13,"Smallest value: $"
    ...
    lea dx, msg4
    mov ah, 9
    int 21h
    

    my teacher ask us to make another one that input 3 numbers and display the smallest value.

    Simply change all of those jng (jump on not greater) instructions by the jnl (jump on not less) instruction.


    This is a slightly better version of your code and using jnl:

     mov bl, num1
     cmp bl, num2
     jnl number2 
     cmp bl, num3
     jnl number3
     mov dl, num1
     jmp Print
    number2:
     mov bl, num2
     cmp bl, num3
     jnl number3
     mov dl, num2
     jmp Print
    number3:
     mov dl, num3
    Print:
     mov ah, 2    
     int 21h
     ret
    

    Good luck monday!