Min and max from numbers read assembly
Solution 1:
Your assembly code seems correct! It does contain a lot of redundant instructions though.
Your solution that works from 5 numbers pushed on the stack deserves a loop:
pop ax ; Consider 5th number as current max
mov cx, 4
TheLoop:
pop dx ; 4th, 3rd, 2nd, and 1st number
cmp dx, ax
jng NotGreater
mov ax, dx ; Set new max
NotGreater:
loop TheLoop
call displayNr
Working from memory based variables
But I can't assign as I thought like
mov max,ax
When you define your max variable like max db ?
, the assembler will complain about mismatching sizes. Just define all your variables as words:
max dw ?
min dw ?
aux dw ?
Pay attention to how you define your labels! The code already has a max label to denote some message (max db "Max :$"
). See in below snippet how I solved this.
It's possible to obtain both the maximum and the minimum from a single loop.
Next code also demonstrates the use of memory based variables although working from registers would be more efficient (as would refraining from using the slow LOOP
instruction)!
mov cx, 5
TheLoop:
pop ax ; 5th, 4th, 3rd, 2nd, and 1st number
cmp ax, max
jng NotGreater
mov max, ax ; Set new maximum
NotGreater:
cmp ax, min
jnl NotLess
mov min, ax ; Set new minimum
NotLess:
loop TheLoop
mov dx, offset msgMax
mov ah, 09h
int 21h
max ax, max
call displayNr
mov dx, offset msgMin
mov ah, 09h
int 21h
max ax, min
call displayNr
...
max dw -32768 ; Smallest word integer
min dw 32767 ; Biggest word integer
msgMax db "Max = $"
msgMin db 13, 10, "Min = $"