Search code examples
assemblyx86masm32-bitirvine32

How to find smallest and largest numbers input by user


So my assignment was to " Write a program that reads numbers until -999 has been entered and then displays the largest and smallest numbers that have been entered"

I have gotten about 75% of this constructed on my own, I keep getting a bug at the very end of my code It keeps displaying 0 as my biggest input. I think this is something to do with the way my counter is set up. I think its trying to pull from memory on my stack that existed before I pushed any.

The second part of this assignment I have absoluteley no idea how to address. If I had a sceond stack to access I could resolve it no problem but I dont so ive been tryign to figure out how to use an array like a stack. I really dont remeber the details but I know it can be done.

INCLUDE Irvine32.inc

.data
input byte 50 DUP(0)
counter Dword 50 dup (0)
counter2 byte 50 dup (0)
array Word 50 dup(0)

sentinal dword (0)

statement  byte "PLease enter a number"  ,0
statement2 byte "To exit enter the number '-999'" ,0
statement3 byte "your biggest number was " ,0


.code
main proc

mov ecx ,1
L1: 
mov ecx ,2
mov edx , OFFSET statement          ; puts statement into register
call WriteString                    
call CRLF
mov edx , offset statement2
Call writestring
Call CRLF

call Readdec
.if (EAX==sentinal )                ;tests agaisnt breakout
sub ecx , 1
.endif
push EAX
inc counter
loop L1

mov ecx ,0
mov ecx , counter

pop EDX                             ;pulls initial inputs
Pop EBX
L2:

.if (EDX > EBX)                     ;test for largest input
pop EbX 
.Elseif (EDX<EBX)
pop EDX
.Endif
loop L2

mov EDX ,offset statement3
call writestring                    ;displays largest input(BUG)
.if (EDX>EBX)
mov edx , eax
CAll writeDec
.elseif (EBX>EDX)
mov eBx , EAX
CAll Writedec
call crlf
.endif
Exit
main endp
end main

Solution

  • I keep getting a bug at the very end of my code It keeps displaying 0 as my biggest input.

    By the time you are ready to print the result you have destroyed the contents of EDX through assigning it the address for the accompanying text.
    Save EDX on the stack:

    push edx
    mov  EDX ,offset statement3
    call writestring                    ;displays largest input(BUG)
    pop  edx
    .if  (EDX>EBX)
    mov  edx , eax
    CAll writeDec
    

    Shouldn't you define sentinal with its correct value?

    sentinal dword (-999)
    

    Can you see that in the next snippet the first line is superfluous? You immediately overwrite the ECX register with another value.

    mov ecx ,1
    L1: 
    mov ecx ,2
    

    Why did you define that many counters? Perhaps strip the 50 dup

    counter Dword 50 dup (0)