Hi, it would be a great help if someone could add some comments on the code below explaining what certain pieces mean etc as it i dont understand any of it. Thanks a lot
: [/blue][code]
: iNumInput equ 30 ; size of input buffers
:
: data segment
: sInNum db "Please input a series of numbers. ->$"
: sNum db "The largest number is: $"
: sInputs db "Number of numbers to compare: $"
: sCompare db "Comparing numbers...$"
: sDone db "Press any key to exit...$"
: count dw 0 ; storage for counting
: numInts dw 0 ; number of ints input
: integer dw 0 ; storage for integer
: prevInt dw 0 ; previous integer for comparing
: res dw 0 ; result (largest interger)
: inputBuf db iNumInput, ?, iNumInput dup ('$'), '$' ; input buffer
: numbrs dw iNumInput dub ('?') ; integer array
: ends
:
: stack segment
:
: dw 128 dup(0)
: ends
:
:
: ;******************************************
: ;
: ; Execution begins here
: ;
: ;******************************************
:
: code segment
: start:
: mov ax, data ; setup registers
: mov ds, ax
: mov es, ax
: lea dx, sInNum ; print string to get input
: call prntStr
: call print_nl
: lea dx, inputBuf ; get input
: call scanStr
: call print_nl
: lea dx, sCompare
: call prntStr
:
: ;--------------------------
: ; Convert chars to integers
: ;--------------------------
: ascii:
: pusha
: lea bx, inputBuf+2 ; actual start of buffer
: xor dx, dx
: mov cx, 0 ; counter offset (index counter)
:
: loop1: ;..This would be better with loopze instruction..
: mov dl, [bx]
: inc bx ; move to next char
: cmp dl, '$' ; end of string?
: je loop1end
: cmp dl, '0' ; Insure character is valid
: je prnt
: cmp dl, '1'
: je prnt
: cmp dl, '2'
: je prnt
: cmp dl, '3'
: je prnt
: cmp dl, '4'
: je prnt
: cmp dl, '5'
: je prnt
: cmp dl, '6'
: je prnt
: cmp dl, '7'
: je prnt
: cmp dl, '8'
: je prnt
: cmp dl, '9'
: je prnt
: ; not a valid number, so skip it
: jmp loop1
: prnt: ; convert char to integer word
: sub dl, 48 ; ASCII chars begin at decimal=48
: push bx
: inc cx
: lea bx, numbrs
: add bx, cx
: mov [bx], dl
: pop bx
: jmp loop1
: loop1end:
: mov [count], cx
: popa
:
: ;-------------------------
: ; Perform actual computing
: ;-------------------------
:
: compute:
: pusha
: mov dx, 0 ; current integer
: mov cx, [count] ; count=number of words in array
: lea bx, numbrs+1
:
: loop2: ; ..would be better with loopze instruction..
:
: ;------------------------
: ; .Convert chars into workable integer
: ;------------------------
: dec cx ; no more chars?
: cmp cx, 0
: je loop2exit
: mov ax, [bx] ; get first digit
: mov dh, al ; swap low order/high order words (integers stored in little-endian)
: mov dl, ah
: mov ax, dx
: mov [integer], dx ; store our integer
: mov dl, 0 ; AH = tens place, AL=ones place (DX=AX)
: mov dh, 10
: mov al, 0
: mul dx ; multiply by dx (DX=10 for tens place)
: mov ax, dx
: mov dx, [integer] ; now second digit (ones place)
: mov dh, 0
: add ax, dx ; Add ones place
: mov [integer], ax ; integer = actual integeral value to compare
:
: ;-----------------------
: ; .Test for largest number
: ;-----------------------
:
: mov dl, [loopTest] ; test for first time looping
: cmp dl, 0
: je loop3first
: jmp compare
: loop3first:
: mov [prevInt], ax ; first tme looping, so prevInt=curInt
: mov [res], ax
: mov [loopTest], 1 ; set flag
: compare:
: inc [numInts] ; ...one more integer
: mov dx, [integer] ; is current int larger then previous?
: mov ax, [prevInt]
: cmp dx, ax
: jge greater
: jmp less
: greater:
: ; ax is bigger, but bigger then res?
: mov ax, dx
: mov ax, [res]
: cmp ax, dx
: jge largest
: mov [res], dx
: mov [integer], ax
: jmp loop2end
: largest:
: mov [res], ax ; yep--ax is largest
: jmp loop2end
: less:
: mov [integer], dx ; nope--dx must be largest
:
: ;----------------------
: ; .Go to next char
: ;----------------------
: loop2end:
: mov [prevInt], dx ; store int to become next prev int
: inc bx ; jump to next number (got to inc on word bonderies)
: inc bx
: jmp loop2
: loop2exit:
: popa
:
: ;---------------------
: ; Print our answer
: ;---------------------
: prntRes:
: pusha
: call print_nl
: lea dx, sInputs
: call prntStr
: call print_nl
: mov ax, [numInts] ; numInts contains number of digits for each char..
: inc ax
: mov bl, 02h ;...because each int is 2 digits, we divide it (ignore overflow)
: div bl
: call print_ax
: call print_nl
: lea dx, sNum
: call prntStr
: mov ax, [res]
: call print_ax
: call print_nl
: call print_nl
: lea dx, sDone
: call prntStr
: popa
:
: ;-------------------------
: ; Exit back to OS
: ;-------------------------
: exit:
: call getch ; wait for quit
: mov ax, 4c00h ; return with error code 0
: int 21h
:
: ends
:
: loopTest db 0
:
: ;********************************
: ;
: ; Print integer as string (AX=integer)
: ;
: ;********************************
: print_ax proc
: cmp ax, 0
: jne print_ax_r
: push ax
: mov al, '0'
: mov ah, 0eh
: int 10h
: pop ax
: ret
: print_ax_r:
: pusha
: mov dx, 0
: cmp ax, 0
: je pn_done
: mov bx, 10
: div bx
: call print_ax_r
: mov ax, dx
: add al, 30h
: mov ah, 0eh
: int 10h
: jmp pn_done
: pn_done:
: popa
: ret
: print_ax endp
:
:
: ;*******************************************
: ;
: ; Prnt char (DL=char to print)
: ;
: ;*******************************************
: prntChar proc near
: mov ah, 02h
: int 21h
: ret
: prntChar endp
:
:
: ;*******************************************
: ;
: ; Prnt $-terminating string (DS:DX=>string)
: ;
: ;*******************************************
: prntStr proc near
: mov ah, 9
: int 21h
: ret
: prntStr endp
:
: ;*******************************************
: ;
: ; Prnt newline/CR
: ;
: ;*******************************************
: print_nl proc
: push ax
: push dx
: mov ah, 2
: mov dl, 0Dh
: int 21h
: mov dl, 0Ah
: int 21h
: pop dx
: pop ax
: ret
: print_nl endp
:
:
: ;*******************************************
: ;
: ; Get input into buffer (DS:DX=>buffer)
: ; First byte in buffer = number of chars to read
: ; ret number of chars read stored in second byte in buffer
: ;
: ;*******************************************
: scanStr proc near
: mov ah, 0ah
: int 21h
: ret
: scanStr endp
:
: ;*******************************************
: ;
: ; Wait for any key press
: ;
: ;*******************************************
: getch proc near
: mov ah, 1
: int 21h
: ret
: getch endp
:
:
: end start ; set entry point and stop the assembler.
:
:
: [/code][blue]