News:

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

Main Menu

New to Masm32 and trying to find the largest

Started by Daryl, February 20, 2014, 11:50:07 PM

Previous topic - Next topic

Daryl

Hello everyone im new to masm32 im trying to get this code working but to no avail im trying to find the largest of the five inputs so far im stuck and i don't know what to do could someone help me

.386
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib


.data


            Message1 db "first number: ", 0
            Message2 db "second number : ", 0
Message3 db "Third number : ", 0
Message4 db "Four number : ", 0
Message5 db "Fifth number : ", 0
            Message6 db "The Largest Number is:",0

.data?

            Number1 dd 10 dup(?)
            Number2 dd 10 dup(?)
Number3 dd 10 dup(?)
Number4 dd 10 dup(?)
Number5 dd 10 dup(?)

.code
start:

        invoke StdOut, addr Message1
        invoke StdIn, addr Number1, 100
        invoke StripLF, addr Number1
        invoke StdOut, addr Message2
        invoke StdIn, addr Number2, 100
        invoke StripLF, addr Number2
        invoke StdOut, addr Message3
        invoke StdIn, addr Number3, 100
        invoke StripLF, addr Number3
        invoke StdOut, addr Message4
        invoke StdIn, addr Number4, 100
        invoke StripLF, addr Number4
        invoke StdOut, addr Message5
        invoke StdIn, addr Number5, 100
        invoke StripLF, addr Number5



         
     

        invoke atodw, addr Number1
        invoke atodw, addr Number2
        mov eax, Number1
        mov ebx,Number2
        cmp eax,ebx
        jge N1
        mov Number1,ebx
        invoke StdOut, addr Message6
         invoke StdOut, addr Number1
        jmp NUM2

   N1:
        mov Number2,eax
        invoke StdOut, addr Message6
        invoke StdOut, addr Number2
         
  NUM2:
   
        invoke atodw, addr Number1
        invoke atodw, addr Number3
        mov eax, Number1
        mov ebx,Number3
        cmp eax,ebx
        jge N2
        mov Number1,ebx
        invoke StdOut, addr Message6
        invoke StdOut, addr Number1
        jmp NUM3
N2:
        mov Number3,eax
        invoke StdOut, addr Message6
        invoke StdOut, addr Number3

NUM3:
   
        invoke atodw, addr Number1
        invoke atodw, addr Number3
        mov eax, Number1
        mov ebx,Number4
        cmp eax,ebx
        jge N4
        mov Number1,ebx
        invoke StdOut, addr Message6
        invoke StdOut, addr Number1
        jmp NUM4
N4:
        mov Number4,eax
        invoke StdOut, addr Message6
        invoke StdOut, addr Number4

NUM4:
   
        invoke atodw, addr Number1
        invoke atodw, addr Number3
        mov eax, Number1
        mov ebx,Number5
        cmp eax,ebx
        jge N5
        mov Number1,ebx
        invoke StdOut, addr Message6
        invoke StdOut, addr Number1
        jmp EXT
N5:   
        mov Number5,eax
        invoke StdOut, addr Message6
        invoke StdOut, addr Number5

EXT:


        invoke ExitProcess, 0
       
end start

jj2007

Hi,

Here is a little template for testing the logic. Note that registers eax, ecx, edx do not "survive" a call to atodw, therefore the use of ebx and esi.

Welcome to the forum.

include \masm32\include\masm32rt.inc

.code
Num1 db "123", 0 ; for testing, we simulate the inputs as strings
Num2 db "555", 0
Num3 db "456", 0
Num4 db "888", 0 ; <<< there it is
Num5 db "789", 0

start:
  mov ebx, -1 ; the counter
  xor esi, esi ; lowest possible number
  invoke atodw, addr Num1
  .if  eax>esi
xchg eax, esi
mov ebx, 1
  .endif
  invoke atodw, addr Num2
  .if  eax>esi
xchg eax, esi
mov ebx, 2
  .endif
  invoke atodw, addr Num3
  .if  eax>esi
xchg eax, esi
mov ebx, 3
  .endif
  invoke atodw, addr Num4
  .if  eax>esi
xchg eax, esi
mov ebx, 4
  .endif
  invoke atodw, addr Num5
  .if  eax>esi
xchg eax, esi
mov ebx, 5
  .endif
  print str$(esi), " is the highest number, and it is #"
  inkey str$(ebx)
  exit

end start

Daryl

im trying to understand the code and this what i came up with i was wondering if you could help me out and i do have few error on the way  like  when i tried to to this

first number : 60
second number: 71
third number: 80
fourth number: 90
fifth number:100

and the output is like this
the largest number:71 the largest number:71 the largest number:100 what did i do wrong ? help me please

.386
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib


.data


            Message1 db "First number: ", 0
            Message2 db "Second number : ", 0
       Message3 db "Third number : ", 0
            Message4 db "Four number : ", 0
            Message5 db "Fifth number : ", 0
            Message6 db "The Largest Number is:",0

.data?

          Number1 dd 10 dup(?)
          Number2 dd 10 dup(?)
Number3 dd 10 dup(?)
Number4 dd 10 dup(?)
Number5 dd 10 dup(?)

.code
start:

        invoke StdOut, addr Message1
        invoke StdIn, addr Number1, 100
        invoke StripLF, addr Number1
        invoke StdOut, addr Message2
        invoke StdIn, addr Number2, 100
        invoke StripLF, addr Number2
        invoke StdOut, addr Message3
        invoke StdIn, addr Number3, 100
        invoke StripLF, addr Number3
        invoke StdOut, addr Message4
        invoke StdIn, addr Number4, 100
        invoke StripLF, addr Number4
        invoke StdOut, addr Message5
        invoke StdIn, addr Number5, 100
        invoke StripLF, addr Number5


         
     

        invoke atodw, addr Number1
        invoke atodw, addr Number2
        mov eax,Number1
        mov ebx,Number2
        CMP eax,ebx
        JG N1
        mov Number1,ebx
        mov Number2,eax
        jmp NUM1
       

   N1:
       
        invoke StdOut, addr Message6
        invoke StdOut, addr Number1

  NUM1:
        invoke atodw, addr Number1
        invoke atodw, addr Number3
        mov eax,Number1
        mov ebx,Number3
        CMP eax,ebx
        JG N2
        mov Number1,ebx
        mov Number3,eax
        jmp NUM2
           
   N2:
       
        invoke StdOut, addr Message6
        invoke StdOut, addr Number1
   
   NUM2:
        invoke atodw, addr Number1
        invoke atodw, addr Number4
        mov eax,Number1
        mov ebx,Number4
        CMP eax,ebx
        JG N3
        mov Number1,ebx
        mov Number4,eax
        jmp NUM3

    N3:
        invoke StdOut, addr Message6
        invoke StdOut, addr Number1

  NUM3:
        invoke atodw, addr Number1
        invoke atodw, addr Number5
        mov eax,Number1
        mov ebx,Number5
        CMP eax,ebx
        mov Number1,ebx
        mov Number4,eax
        JG N4
    N4:
        invoke StdOut, addr Message6
        invoke StdOut, addr Number1

EXT:


        invoke ExitProcess, 0
       
end start

jj2007

Quote from: Daryl on February 21, 2014, 01:13:52 AMi can't really use if or else for now.

  xor esi, esi ; lowest possible number
  invoke atodw, addr Num1
  cmp eax, esi
  jb @F
  xchg eax, esi
  mov ebx, 1
  @@:
  invoke atodw, addr Num2
  cmp eax, esi
  jb @F
  xchg eax, esi
  mov ebx, 2
  @@:
etc.

Daryl

i already got my it running but still some i think major error cause when i tried to input
this
first number : 60
second number: 71
third number: 80
fourth number: 90
fifth number:100

and the output is like this
the largest number:71 the largest number:71 the largest number:100 what did i do wrong ? help me please


.386
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib


.data


            Message1 db "First number: ", 0
            Message2 db "Second number : ", 0
       Message3 db "Third number : ", 0
            Message4 db "Four number : ", 0
            Message5 db "Fifth number : ", 0
            Message6 db "The Largest Number is:",0

.data?

          Number1 dd 10 dup(?)
          Number2 dd 10 dup(?)
Number3 dd 10 dup(?)
Number4 dd 10 dup(?)
Number5 dd 10 dup(?)

.code
start:

        invoke StdOut, addr Message1
        invoke StdIn, addr Number1, 100
        invoke StripLF, addr Number1
        invoke StdOut, addr Message2
        invoke StdIn, addr Number2, 100
        invoke StripLF, addr Number2
        invoke StdOut, addr Message3
        invoke StdIn, addr Number3, 100
        invoke StripLF, addr Number3
        invoke StdOut, addr Message4
        invoke StdIn, addr Number4, 100
        invoke StripLF, addr Number4
        invoke StdOut, addr Message5
        invoke StdIn, addr Number5, 100
        invoke StripLF, addr Number5


         
     

        invoke atodw, addr Number1
        invoke atodw, addr Number2
        mov eax,Number1
        mov ebx,Number2
        CMP eax,ebx
        JG N1
        mov Number1,ebx
        mov Number2,eax
        jmp NUM1
       

   N1:
       
        invoke StdOut, addr Message6
        invoke StdOut, addr Number1

  NUM1:
        invoke atodw, addr Number1
        invoke atodw, addr Number3
        mov eax,Number1
        mov ebx,Number3
        CMP eax,ebx
        JG N2
        mov Number1,ebx
        mov Number3,eax
        jmp NUM2
           
   N2:
       
        invoke StdOut, addr Message6
        invoke StdOut, addr Number1
   
   NUM2:
        invoke atodw, addr Number1
        invoke atodw, addr Number4
        mov eax,Number1
        mov ebx,Number4
        CMP eax,ebx
        JG N3
        mov Number1,ebx
        mov Number4,eax
        jmp NUM3

    N3:
        invoke StdOut, addr Message6
        invoke StdOut, addr Number1

  NUM3:
        invoke atodw, addr Number1
        invoke atodw, addr Number5
        mov eax,Number1
        mov ebx,Number5
        CMP eax,ebx
        mov Number1,ebx
        mov Number4,eax
        JG N4
    N4:
        invoke StdOut, addr Message6
        invoke StdOut, addr Number1

EXT:


        invoke ExitProcess, 0
       
end start

jj2007

        invoke StdOut, addr Message6
        invoke StdOut, addr Number1
  ; Do you want it to continue with NUM1?? If not:
    jmp @ByeBye
  NUM1:

RuiLoureiro

Quote from: Daryl on February 21, 2014, 02:19:34 AM
i already got my it running but still some i think major error cause when i tried to input
this
first number : 60
second number: 71
third number: 80
fourth number: 90
fifth number:100

and the output is like this
the largest number:71 the largest number:71 the largest number:100 what did i do wrong ? help me please


.386
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib


.data


            Message1 db "First number: ", 0
            Message2 db "Second number : ", 0
       Message3 db "Third number : ", 0
            Message4 db "Four number : ", 0
            Message5 db "Fifth number : ", 0
            Message6 db "The Largest Number is:",0

.data?

          Number1 dd 10 dup(?)
          Number2 dd 10 dup(?)
Number3 dd 10 dup(?)
Number4 dd 10 dup(?)
Number5 dd 10 dup(?)

.code
start:

        invoke StdOut, addr Message1
        invoke StdIn, addr Number1, 100
        invoke StripLF, addr Number1
        invoke StdOut, addr Message2
        invoke StdIn, addr Number2, 100
        invoke StripLF, addr Number2
        invoke StdOut, addr Message3
        invoke StdIn, addr Number3, 100
        invoke StripLF, addr Number3
        invoke StdOut, addr Message4
        invoke StdIn, addr Number4, 100
        invoke StripLF, addr Number4
        invoke StdOut, addr Message5
        invoke StdIn, addr Number5, 100
        invoke StripLF, addr Number5


         
     

        invoke atodw, addr Number1
        invoke atodw, addr Number2
        mov eax,Number1
        mov ebx,Number2
        CMP eax,ebx
        JG N1
        mov Number1,ebx
        mov Number2,eax
        jmp NUM1
       

   N1:
       
        invoke StdOut, addr Message6
        invoke StdOut, addr Number1

  NUM1:
        invoke atodw, addr Number1
        invoke atodw, addr Number3
        mov eax,Number1
        mov ebx,Number3
        CMP eax,ebx
        JG N2
        mov Number1,ebx
        mov Number3,eax
        jmp NUM2
           
   N2:
       
        invoke StdOut, addr Message6
        invoke StdOut, addr Number1
   
   NUM2:
        invoke atodw, addr Number1
        invoke atodw, addr Number4
        mov eax,Number1
        mov ebx,Number4
        CMP eax,ebx
        JG N3
        mov Number1,ebx
        mov Number4,eax
        jmp NUM3

    N3:
        invoke StdOut, addr Message6
        invoke StdOut, addr Number1

  NUM3:
        invoke atodw, addr Number1
        invoke atodw, addr Number5
        mov eax,Number1
        mov ebx,Number5
        CMP eax,ebx
        mov Number1,ebx
        mov Number4,eax
        JG N4
    N4:
        invoke StdOut, addr Message6
        invoke StdOut, addr Number1

EXT:


        invoke ExitProcess, 0
       
end start


Hi Daryl
           I dont know what you want to do,
           but

           ** First, define this

.data?
            StringNumber1 dd 10 dup(?)        ; <-- get the digits to here
            StringNumber2 dd 10 dup(?)
           StringNumber3 dd 10 dup(?)
           StringNumber4 dd 10 dup(?)
           StringNumber5 dd 10 dup(?)
and this:
          Number1 dd ?                      ; <-- convert StringNumber1 to here
          Number2 dd ?
          Number3 dd ?
          Number4 dd ?
          Number5 dd ?

           ** Second, convert each StringNumber1 to Number1, StringNumber2 to Number2, etc

           ** Third, compare the numbers as you want or ...

                mov     eax, Number1
                cmp     eax, ...        ; Number2 ?
               
          Why not ?

Daryl

well all i just want in my output to show only one biggest number out of five inputs but sadly  keep showing  this

first number : 60
second number: 71
third number: 80
fourth number: 90
fifth number:100

the largest number:71 the largest number:71 the largest number:100

i what i really want is the it show the highest number out for five numbers

the largest number:100

jj2007


Daryl

Thanks that solve my problem for output but i got a question do i need to do four more jump and 4 more compare instruction for second input third input and four input and fifth input? and when i tried to input

first number: 10
second number: 10
third number: 5
four number: 30
fifth number 40

the largest number is : 10 what did i do wrong ?

TWell

Quote from: Daryl on February 21, 2014, 06:21:37 PM
what did i do wrong ?
for example this        invoke atodw, addr Number1
        invoke atodw, addr Number2
EAX from first call is not used at all?

MichaelW

Any way you do it multiple comparisons will be required. One method of finding the largest value is to enter all of the values into an array and then sort the array. I used the CRT qsort function as a convenience, but there are many sorting algorithms available and some, a bubble sort for example, are easy to understand and implement.

;==================================================================================================
include \masm32\include\masm32rt.inc
;==================================================================================================
.data
    n1 dd 0
    n2 dd 0
    n3 dd 0
    n4 dd 0
    n5 dd 0
.code
;==================================================================================================
compare proc C ptr1:DWORD, ptr2:DWORD
    ;----------------------------------------------------------
    ; This comparison function for the CRT qsort function will
    ; cause the array to be sorted in increasing order, so the
    ; element with the highest value will be the last element.
    ;----------------------------------------------------------
    mov eax, ptr1
    mov eax, [eax]
    mov edx, ptr2
    mov edx, [edx]
    sub eax, edx
    ret
compare endp
;==================================================================================================
start:
;==================================================================================================
    invoke crt_scanf, cfm$("%d%d%d%d%d"), ADDR n1, ADDR n2, ADDR n3, ADDR n4, ADDR n5
    invoke crt_qsort, ADDR n1, 5, 4, compare
    printf("\nlargest = %d\n\n", n5)
    inkey
    exit
;==================================================================================================
end start


Well Microsoft, here's another nice mess you've gotten us into.

RuiLoureiro

#12
Quote from: Daryl on February 21, 2014, 06:21:37 PM
Thanks that solve my problem for output but i got a question do i need to do four more jump and 4 more compare instruction for second input third input and four input and fifth input? and when i tried to input

first number: 10
second number: 10
third number: 5
four number: 30
fifth number 40

the largest number is : 10 what did i do wrong ?
The answer is this:

When we start doing wrong things, we have a lot of problems and we have a lot of problems to get the right solution at the end.

RuiLoureiro

Hi Daryl,
        I dont like to use CONSOLE assemble ...
        Meanwhile, you can try to learn with
        the following example
        (i tried to use your code)

        Good luck !  :t
       
Results:

Example I
-------------
First number: 12
Second number : 13
Third number : 14
Four number : 15
Fifth number : 16
The Largest Number is:13
The Largest Number is:14
The Largest Number is:15
The Largest Number is:16

Example II
--------------
First number: 17
Second number : 71
Third number : 15
Four number : 90
Fifth number : 100
The Largest Number is:71
The Largest Number is:90
The Largest Number is:100
-------------------------------------

.386
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib

copyTostrX      proto   :DWORD
;----------------------------------------------------
.data

            Message1        db "First number: ", 0
            Message2        db "Second number : ", 0
      Message3        db "Third number : ", 0
            Message4        db "Four number : ", 0
            Message5        db "Fifth number : ", 0
           
            MessageLargest  db "The Largest Number is:",0
.data?
            StringNumber1   dd 7 dup(?)
            StringNumber2   dd 7 dup(?)
StringNumber3   dd 7 dup(?)
StringNumber4   dd 7 dup(?)
StringNumber5   dd 7 dup(?)

StringNumberX   dd 7 dup(?)           ; for each StringNumber? without LF

            Number1         dd ?
            Number2         dd ?
            Number3         dd ?
            Number4         dd ?
            Number5         dd ?

.code
start:

        ;--------------------------------
        ;   get each string number
        ;--------------------------------
        invoke StdOut, addr Message1
        invoke StdIn, addr StringNumber1, 20       

        invoke StdOut, addr Message2
        invoke StdIn, addr StringNumber2, 20

        invoke StdOut, addr Message3
        invoke StdIn, addr StringNumber3, 20

        invoke StdOut, addr Message4
        invoke StdIn, addr StringNumber4, 20       
       
        invoke StdOut, addr Message5
        invoke StdIn, addr StringNumber5, 20

        ;------------------------------------------
        ; copy each string number to StringNumberX
        ;           and strip LF
        ;        convert it to number
        ; note: we need each StringNumber1 etc
        ;       as they are
        ;-----------------------------------------
        invoke      copyTostrX, addr StringNumber1
        invoke      atodw,  addr StringNumberX       
        mov         Number1, eax
         
        invoke      copyTostrX, addr StringNumber2
        invoke      atodw,  addr StringNumberX       
        mov         Number2, eax

        invoke      copyTostrX, addr StringNumber3
        invoke      atodw,  addr StringNumberX       
        mov         Number3, eax

        invoke      copyTostrX, addr StringNumber4
        invoke      atodw,  addr StringNumberX       
        mov         Number4, eax

        invoke      copyTostrX, addr StringNumber5
        invoke      atodw,  addr StringNumberX       
        mov         Number5, eax

        ;--------------------------------
        ;     compare numbers
        ;--------------------------------
        mov     edi, offset StringNumber1       ; this is the largest
       
        mov     ebx, Number1                    ; this is the largest = ebx
        mov     eax, Number2

        CMP     ebx, eax
        JGE     NUM3                            ; ebx is the largest
       
        mov     ebx, eax
        mov     edi, offset StringNumber2
       
        invoke  StdOut, addr MessageLargest     ; this preserves EBX,ESI,EDI
        invoke  StdOut, edi                     ; this preserves EBX,ESI,EDI

  NUM3:
        mov     eax,Number3
        CMP     ebx, eax
        JGE     NUM4                            ; ebx is the largest

        mov     ebx, eax
        mov     edi, offset StringNumber3
                           
        invoke  StdOut, addr MessageLargest
        invoke  StdOut, edi
   
   NUM4:
        mov     eax,Number4
        CMP     ebx, eax
        JGE     NUM5                            ; ebx is the largest
       
        mov     ebx, eax
        mov     edi, offset StringNumber4

        invoke  StdOut, addr MessageLargest
        invoke  StdOut, edi

  NUM5:
        mov     eax,Number5
        CMP     ebx, eax                        ; ebx is the largest
        JGE     FINISH

        mov     ebx, eax
        mov     edi, offset StringNumber5
               
FINISH:
        ;----------------------------------
        ;    the largest is the EBX
        ;----------------------------------
        invoke  StdOut, addr MessageLargest
        invoke  StdOut, edi

_EXIT:
        jmp     _EXIT

        invoke ExitProcess, 0
;-----------------------------------------------------
copyTostrX      proc        pStr:DWORD

                mov         ecx, pStr
                mov         edx, offset StringNumberX
               
        @@:     movzx       eax, byte ptr [ecx]     
                mov         [edx], al
                or          eax, eax
                jz          short @F
               
                add         ecx, 1
                add         edx, 1
                jnz         short @B               

        @@:     invoke  StripLF, addr StringNumberX
                ret
copyTostrX      endp       
;-----------------------------------------------------
end start
---------------------------------------
; Example I
;----------
;First number: 12
;Second number : 13
;Third number : 14
;Four number : 15
;Fifth number : 16
;The Largest Number is:13
;The Largest Number is:14
;The Largest Number is:15
;The Largest Number is:16

; Example II
;-----------
;First number: 17
;Second number : 71
;Third number : 15
;Four number : 90
;Fifth number : 100
;The Largest Number is:71
;The Largest Number is:90
;The Largest Number is:100