Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

find max of 3 numbers

RodeoBurnzRodeoBurnz Member Posts: 47
[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
pushad
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
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]


Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : [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]
    : pushad
    : 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]
  • RodeoBurnzRodeoBurnz Member Posts: 47
    : : [code]
    : : .CODE ; program code
    : :
    : : Max3 PROC NEAR32
    : : push ebx ; save registers
    : : push ecx
    : : [red][b]push eax[/red][/b]
    : : pushad
    : : 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.

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : : : [code]
    : : : .CODE ; program code
    : : :
    : : : Max3 PROC NEAR32
    : : : push ebx ; save registers
    : : : push ecx
    : : : [red][b]push eax[/red][/b]
    : : : pushad
    : : : 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:

    instead of:

    [b]PUBLIC _start
    END[/b]

    you should have only:

    [b]END _start[/b]
    [/blue]
  • Justin BibJustin Bib USAMember Posts: 0

    ______ | http://forcoder.org | free ebooks and video tutorials about [ JavaScript C# Ruby C Visual Basic PL/SQL PHP Assembly Go Python Perl Swift Delphi R MATLAB Scratch Java Visual Basic .NET Objective-C C++ Hack Scheme Prolog Fortran Dart Transact-SQL Erlang VBScript Lisp Logo Apex ABAP SAS Alice Kotlin Julia Bash LabVIEW Ada F# Rust Scala D Clojure COBOL FoxPro Crystal Lua Awk ML ] ____

Sign In or Register to comment.