News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Compare which number is greater

Started by rodolfolopes, October 21, 2014, 08:20:58 AM

Previous topic - Next topic

rodolfolopes

Hi there guys

The other time I asked help to know how to do a program how to know if the number given by the user is greater or less than 10

But Now i gotta know which number is greater but with the user input for exampleÑ

Give me the first number: 5
Give me the second number: 14

14 is greater



Does anyone has an idea how to do this?

Thanks guys


Gunther

What about a simple compare of both numbers? Your question sounds like homework. We won't do that for you.

Gunther
You have to know the facts before you can distort them.

jj2007

Quote from: rodolfolopes on October 21, 2014, 08:20:58 AM
Give me the first number: 5
Give me the second number: 14

Look in \Masm32\Help\*.chm for:
- console input functions
- ascii to dword conversion
- the cmp and jg instructions (opcodes.chm)

Don´t forget you need non-volatile registers (esi, edi, ebx) to store the first number - getting the second number will trash eax ecx edx

rodolfolopes

Quote from: jj2007 on October 21, 2014, 09:20:37 AM
Quote from: rodolfolopes on October 21, 2014, 08:20:58 AM
Give me the first number: 5
Give me the second number: 14

Look in \Masm32\Help\*.chm for:
- console input functions
- ascii to dword conversion
- the cmp and jg instructions (opcodes.chm)

Don´t forget you need non-volatile registers (esi, edi, ebx) to store the first number - getting the second number will trash eax ecx edx



Sorry guys Yeah basically its my today homework but I just needed another example than mine.

I have been trying this code but its now working
It always tells me that the second number is greater!!

ANy ideas!



include \masm32\include\masm32rt.inc


.code
start:


mov esi, input("GIVE ME THE FIRST NUMBER: ",62," ")
mov ecx, input("GIVE ME THE SECOND NUMBER: ",62," ")
invoke atodw, eax
invoke atodw,esi
   
.if esi < eax
print "FIRST NUMBER IS GREATER ", 13, 10

.elseif esi >eax
print "SECOND NUMBER IS GREATER", 13, 10

.elseif esi==eax
print "NUMBERS ARE EQUAL", 13, 10

.endif

print "-----------------------------------------------------------", 13, 10

exit
end start



habran


; #########################################################################

  ; ---------------------------------------------------------------
  ;      This procedure was originally written by Tim Roberts
  ;
  ; Part of this code has been optimised by Alexander Yackubtchik
  ; ---------------------------------------------------------------

    .486
    .model flat, stdcall  ; 32 bit memory model
    option casemap :none  ; case sensitive
    .code

; #########################################################################

atodw proc String:DWORD

  ; ----------------------------------------
  ; Convert decimal string into dword value
  ; return value in eax
  ; ----------------------------------------

    push esi
    push edi

    xor eax, eax
    mov esi, [String]
    xor ecx, ecx
    xor edx, edx
    mov al, [esi]
    inc esi
    cmp al, 2D
    jne proceed
    mov al, byte ptr [esi]
    not edx
    inc esi
    jmp proceed

  @@:
    sub al, 30h
    lea ecx, dword ptr [ecx+4*ecx]
    lea ecx, dword ptr [eax+2*ecx]
    mov al, byte ptr [esi]
    inc esi

  proceed:
    or al, al
    jne @B
    lea eax, dword ptr [edx+ecx]
    xor eax, edx

    pop edi
    pop esi

    ret

atodw endp

; #########################################################################

end
Cod-Father

dedndave

mov esi, input("GIVE ME THE FIRST NUMBER: ",62," ")
mov ecx, input("GIVE ME THE SECOND NUMBER: ",62," ")
invoke atodw, eax
invoke atodw, esi


EAX, ECX, and EDX are volatile registers across calls
and, the first atodw call returns a value in EAX - which is destroyed by the second atodw call

mov esi, input("GIVE ME THE FIRST NUMBER: ",62," ")
mov ecx, input("GIVE ME THE SECOND NUMBER: ",62," ")
invoke atodw, ecx
xchg eax, esi
invoke atodw, eax


now, the first dword is in EAX and the second dword is in ESI

hutch--

You need to use the mnemonic "CMP" then branch from the result using conditional jumps.


cmp eax, 10
jg greater
je equal
jl lessthan

; more code ....

greater:

equal:

lessthan:


I have used signed comparison evaluation with the conditional jumps, it can also be done in unsigned branching.

Mikl__

hutch--,
cmp eax, 10
jg greater
je equal
jl lessthan

; more code ....

greater:

equal:

lessthan:
.data
table dd greater,lessthan, 62 dup (0),equal
.code
cmp eax, 10
pushf
pop ebx
and ebx,1000001b
jmp table[ebx*4]

hutch--

 :biggrin:

Nice optimisation but I am not sure our friend needs that yet.

Gunther

Quote from: hutch-- on October 21, 2014, 02:50:59 PM
:biggrin:

Nice optimisation but I am not sure our friend needs that yet.

I think it's to sophisticated for him.

Gunther
You have to know the facts before you can distort them.

jj2007

Rodolfo,
Your code is almost OK. You need to move the FIRST result into a non-volatile register like esi (or edi, ebx).

Quote from: dedndave on October 21, 2014, 11:18:33 AMthe first atodw call returns a value in EAX - which is destroyed by the second atodw call

include \masm32\include\masm32rt.inc

.code
start:

  invoke atodw, input("GIVE ME THE FIRST NUMBER: ",62," ")
  xchg eax, esi ; move result to non-volatile register esi

  invoke atodw, input("GIVE ME THE SECOND NUMBER: ",62," ")

  .if esi > eax
print "FIRST NUMBER IS GREATER ", 13, 10

  .elseif esi < eax
print "SECOND NUMBER IS GREATER", 13, 10

  .elseif esi==eax
print "NUMBERS ARE EQUAL", 13, 10

  .endif

  print "-----------------------------------------------------------", 13, 10

  exit
end start


Instead of xchg eax, esi you can use mov esi, eax - but it's one byte longer ;-)