# find max of 3 numbers

[b][red]This message was edited by RodeoBurnz at 2002-10-29 21:24:10[/red][/b][hr]
hi, I'm trying to code a program that asks for three numbers and gives back the biggest and stores it in EAX. My code is below, the logic is correct. But in the output, the number being given back is the last number inputted. I debugged the code, the problem is that when it gets to the push instructions, the value stored in EAX is replaced by the last digit entered. I dont know why it is doing this. Someone please help me. Thank you.

[code]
.CODE ; program code

Max3 PROC NEAR32
push ebx ; save registers
push ecx
push eax
pushf

cmp ecx, ebx ; ecx > ebx ?
jg ifGreat
jl ifLess

ifGreat: cmp ecx, eax ; ecx > eax ?
jg moveReg
jl moveReg3

ifLess: cmp ebx, eax ; ebx > eax?
jg moveReg2
jl moveReg3

moveReg: mov eax, ecx
jmp endProc

moveReg2: mov eax, ebx
jmp endProc

moveReg3: mov eax, eax

endProc: popf ; restore flags
pop eax ; restore EAX
pop ecx
pop ebx
ret
Max3 ENDP

_start:
output prompt1 ; ask for 1st value
input stringIn, 20 ; get value
atod stringIn ; convert to integer
mov ebx, eax ; copy to EBX
output prompt2 ; ask for 2nd value
input stringIn, 20 ; get value
atod stringIn ; convert to integer
mov ecx, eax ; copy to ECX
output prompt3 ; ask for 3rd value
input stringIn, 20 ; get value
atod stringIn ; convert to integer
;mov edx, eax ; copy to EDX
call Max3 ; go to Max3 procedure
dtoa maxVal, eax ; decimal to ASCII
output answer ; output "The largest is: "
output maxVal ; output maximum value

INVOKE ExitProcess, 0 ; exit with return code 0
PUBLIC _start ; make entry point public

END ; end of source code
[/code]

• : [b][red]This message was edited by RodeoBurnz at 2002-10-29 21:24:10[/red][/b][hr]
: hi, I'm trying to code a program that asks for three numbers and gives back the biggest and stores it in EAX. My code is below, the logic is correct. But in the output, the number being given back is the last number inputted. I debugged the code, the problem is that when it gets to the push instructions, the value stored in EAX is replaced by the last digit entered. I dont know why it is doing this. Someone please help me. Thank you.
:
: [code]
: .CODE ; program code
:
: Max3 PROC NEAR32
: push ebx ; save registers
: push ecx
: [red][b]push eax[/red][/b]
: pushf
:
: cmp ecx, ebx ; ecx > ebx ?
: jg ifGreat
: jl ifLess
:
: ifGreat: cmp ecx, eax ; ecx > eax ?
: jg moveReg
: jl moveReg3
:
: ifLess: cmp ebx, eax ; ebx > eax?
: jg moveReg2
: jl moveReg3
:
: moveReg: mov eax, ecx
: jmp endProc
:
: moveReg2: mov eax, ebx
: jmp endProc
:
: moveReg3: mov eax, eax
:
: endProc: popf ; restore flags
: [red][b]pop eax[/red][/b] ; restore EAX
: pop ecx
: pop ebx
: ret
: Max3 ENDP
:
: _start:
: output prompt1 ; ask for 1st value
: input stringIn, 20 ; get value
: atod stringIn ; convert to integer
: mov ebx, eax ; copy to EBX
: output prompt2 ; ask for 2nd value
: input stringIn, 20 ; get value
: atod stringIn ; convert to integer
: mov ecx, eax ; copy to ECX
: output prompt3 ; ask for 3rd value
: input stringIn, 20 ; get value
: atod stringIn ; convert to integer
: ;mov edx, eax ; copy to EDX
: call Max3 ; go to Max3 procedure
: dtoa maxVal, eax ; decimal to ASCII
: output answer ; output "The largest is: "
: output maxVal ; output maximum value
:
: INVOKE ExitProcess, 0 ; exit with return code 0
: PUBLIC _start ; make entry point public
:
: END ; end of source code
: [/code]
:
:
:
[blue]If you need to RETURN EAX - why you PUSH/POP it? Get rid of RED and see if it will fix the problem. Also, PUSHAD contains EAX, EBX, ECX, ... and more, so you duplicating your PUSHes. My advice - code your procedures without saving/restoring regs - big waste.[/blue]
• : : [code]
: : .CODE ; program code
: :
: : Max3 PROC NEAR32
: : push ebx ; save registers
: : push ecx
: : [red][b]push eax[/red][/b]
: : pushf
: :
: : cmp ecx, ebx ; ecx > ebx ?
: : jg ifGreat
: : jl ifLess
: :
: : ifGreat: cmp ecx, eax ; ecx > eax ?
: : jg moveReg
: : jl moveReg3
: :
: : ifLess: cmp ebx, eax ; ebx > eax?
: : jg moveReg2
: : jl moveReg3
: :
: : moveReg: mov eax, ecx
: : jmp endProc
: :
: : moveReg2: mov eax, ebx
: : jmp endProc
: :
: : moveReg3: mov eax, eax
: :
: : endProc: popf ; restore flags
: : popad ; restore registers
: : [red][b]pop eax[/red][/b] ; restore EAX
: : pop ecx
: : pop ebx
: : ret
: : Max3 ENDP
: :
: : _start:
: : output prompt1 ; ask for 1st value
: : input stringIn, 20 ; get value
: : atod stringIn ; convert to integer
: : mov ebx, eax ; copy to EBX
: : output prompt2 ; ask for 2nd value
: : input stringIn, 20 ; get value
: : atod stringIn ; convert to integer
: : mov ecx, eax ; copy to ECX
: : output prompt3 ; ask for 3rd value
: : input stringIn, 20 ; get value
: : atod stringIn ; convert to integer
: : ;mov edx, eax ; copy to EDX
: : call Max3 ; go to Max3 procedure
: : dtoa maxVal, eax ; decimal to ASCII
: : output answer ; output "The largest is: "
: : output maxVal ; output maximum value
: :
: : INVOKE ExitProcess, 0 ; exit with return code 0
: : PUBLIC _start ; make entry point public
: :
: : END ; end of source code
: : [/code]
: :
: :
: :
: [blue]If you need to RETURN EAX - why you PUSH/POP it? Get rid of RED and see if it will fix the problem. Also, PUSHAD contains EAX, EBX, ECX, ... and more, so you duplicating your PUSHes. My advice - code your procedures without saving/restoring regs - big waste.[/blue]
:
i got rid of the push/pop EAX, but when i run the .exe, it says that the file has caused an unknown error and will now close. the same thing happened when i got rid of the pushad/popad also.

• : : : [code]
: : : .CODE ; program code
: : :
: : : Max3 PROC NEAR32
: : : push ebx ; save registers
: : : push ecx
: : : [red][b]push eax[/red][/b]
: : : pushf
: : :
: : : cmp ecx, ebx ; ecx > ebx ?
: : : jg ifGreat
: : : jl ifLess
: : :
: : : ifGreat: cmp ecx, eax ; ecx > eax ?
: : : jg moveReg
: : : jl moveReg3
: : :
: : : ifLess: cmp ebx, eax ; ebx > eax?
: : : jg moveReg2
: : : jl moveReg3
: : :
: : : moveReg: mov eax, ecx
: : : jmp endProc
: : :
: : : moveReg2: mov eax, ebx
: : : jmp endProc
: : :
: : : moveReg3: mov eax, eax
: : :
: : : endProc: popf ; restore flags
: : : popad ; restore registers
: : : [red][b]pop eax[/red][/b] ; restore EAX
: : : pop ecx
: : : pop ebx
: : : ret
: : : Max3 ENDP
: : :
: : : _start:
: : : output prompt1 ; ask for 1st value
: : : input stringIn, 20 ; get value
: : : atod stringIn ; convert to integer
: : : mov ebx, eax ; copy to EBX
: : : output prompt2 ; ask for 2nd value
: : : input stringIn, 20 ; get value
: : : atod stringIn ; convert to integer
: : : mov ecx, eax ; copy to ECX
: : : output prompt3 ; ask for 3rd value
: : : input stringIn, 20 ; get value
: : : atod stringIn ; convert to integer
: : : ;mov edx, eax ; copy to EDX
: : : call Max3 ; go to Max3 procedure
: : : dtoa maxVal, eax ; decimal to ASCII
: : : output answer ; output "The largest is: "
: : : output maxVal ; output maximum value
: : :
: : : INVOKE ExitProcess, 0 ; exit with return code 0
: : : PUBLIC _start ; make entry point public
: : :
: : : END ; end of source code
: : : [/code]
: : :
: : :
: : :
: : [blue]If you need to RETURN EAX - why you PUSH/POP it? Get rid of RED and see if it will fix the problem. Also, PUSHAD contains EAX, EBX, ECX, ... and more, so you duplicating your PUSHes. My advice - code your procedures without saving/restoring regs - big waste.[/blue]
: :
: i got rid of the push/pop EAX, but when i run the .exe, it says that the file has caused an unknown error and will now close. the same thing happened when i got rid of the pushad/popad also.
:
:
[blue]You also have weird [b]_start[/b] declaration. Get rid of PUBLIC and add [b]_start[/b] to the END statement:

[b]PUBLIC _start
END[/b]

you should have only:

[b]END _start[/b]
[/blue]