The MASM Forum

General => The Campus => Topic started by: rodolfolopes on October 21, 2014, 08:20:58 AM

Title: Compare which number is greater
Post by: rodolfolopes on October 21, 2014, 08:20:58 AM
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

Title: Re: Compare which number is greater
Post by: Gunther on October 21, 2014, 09:11:40 AM
What about a simple compare of both numbers? Your question sounds like homework. We won't do that for you.

Gunther
Title: Re: Compare which number is greater
Post by: 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
Title: Re: Compare which number is greater
Post by: rodolfolopes on October 21, 2014, 10:06:47 AM
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


Title: Re: Compare which number is greater
Post by: habran on October 21, 2014, 10:26:23 AM

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

  ; ---------------------------------------------------------------
  ;      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
Title: Re: Compare which number is greater
Post by: dedndave on October 21, 2014, 11:18:33 AM
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
Title: Re: Compare which number is greater
Post by: hutch-- on October 21, 2014, 01:30:56 PM
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.
Title: Re: Compare which number is greater
Post by: Mikl__ on October 21, 2014, 01:55:37 PM
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]
(http://www.cyberforum.ru/images/smilies/smile3.gif)
Title: Re: Compare which number is greater
Post by: hutch-- on October 21, 2014, 02:50:59 PM
 :biggrin:

Nice optimisation but I am not sure our friend needs that yet.
Title: Re: Compare which number is greater
Post by: Gunther on October 21, 2014, 09:07:29 PM
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
Title: Re: Compare which number is greater
Post by: jj2007 on October 22, 2014, 08:41:41 AM
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 ;-)