;=============================================================
; File:    Max.asm
; Purpose: Calculate the maximum of a collection of a
;          collection of numbers input at the keyboard.
;          Show how to use the stack to transmit parameters.
;=============================================================

include 'emu8086.inc'

       org  100h ; set location counter to 100h

       jmp  CodeStart

DataStart:

prompt   db  13, 10, "Next number> ", 0 
newline  db  13, 10, 0
maxMsg   db  13, 10, 13, 10, "The maximum was ", 0
fini     db  13, 10, 13, 10, "Fini", 13, 10, 0
num      dw  ?
maxNum   dw  0

CodeStart:
       mov  si, offset prompt    ; prompt for number
       call print_string
       call scan_num             ; get next number
       mov  num, cx
       push cx                   ; push number onto stack
       mov  ax, maxNum           ; push max onto stack
       push ax
       call Max                  ; call the Max routine
       pop  ax                   ; save new maximum
       mov  maxNum, ax
       cmp  cx, 0                ; check for zero
       jne  CodeStart            ; go back for more
       
       mov  si, offset maxMsg    ; display "Max was"
       call print_string

       mov  ax, maxNum           ; display maximum value
       call print_num

       mov  si, offset fini      ; display "Fini"
       call print_string

       ret                       ; return to caller


; Max(X,Y) takes X and Y from the stack and 
; returns the maximum value on the stack.
Max    PROC
       pop  dx                   ; save return address
       pop  ax                   ; get X
       pop  bx                   ; get Y
       cmp  ax, bx               ; compare X : Y
       jl   AxLess
       push ax                   ; X is larger
       jmp  Rtn
AxLess:
       push bx                   ; Y is larger
Rtn:     
       push dx                   ; push return address onto stack
       ret                       ; return to caller
Max    ENDP

DEFINE_PRINT_STRING
DEFINE_SCAN_NUM
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS
