News:

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

Main Menu

Can someone please help me understand this code line by line ?

Started by EmuRatti39, October 19, 2018, 09:33:11 AM

Previous topic - Next topic

EmuRatti39

If that is not possible , can someone at least tell me what to do to learn it ?

Google line by line ?

.386
.MODEL     flat, stdcall
OPTION     casemap :none



INCLUDE    \masm32\include\masm32rt.inc



cdMaxSize  EQU 11

.DATA?
  szNumber1 db cdMaxSize dup(?)
  szNumber2 db cdMaxSize dup(?)
  iNum      dd ?

.DATA
  szTxtNum1 db "Your first number: ", 0
  szTxtNum2 db "Your second number: ", 0
  szTxtSum  db "Both sum: ", 0
  number1   DD 442
  number2   dd 22
  msg       db cdMaxSize dup(0)

.CODE
  @num2str  proc uses ebx edx esi
    ; In: eax, number value
    ; Out: eax, offset of value in string format
    mov     esi, offset msg + cdMaxSize - 1
    mov     ebx, 10
    @Next:
      dec     esi
      xor     edx, edx
      div     ebx
      or      edx, 30h
      mov     byte ptr [esi], dl
      or      eax, eax
    jnz     @Next
    mov     eax, esi
    ret
  @num2str endp

  str2num proc uses ebx ecx edx esi edi
    ; In: eax, offset to str_num
    ; out: eax, the number converted
    mov     edi, eax              ; offset
    mov     eax, 0
    mov     ecx, cdMaxSize
    repne   scasb
    jne     @NoFound
    sub     edi, 2
    sub     ecx, cdMaxSize
    neg     ecx
    mov     ebx, 1                ; factor
    mov     esi, 0                ; Acumulative
    @Next:
      dec     ecx
      jl      @Exit
      xor     eax, eax            ; clear it
      mov     al, byte ptr [edi]
      and     al, 15              ; 2num
      mul     ebx
      add     esi, eax            ; get into accumulative
      mov     eax, ebx            ; increases factor
      mov     ebx, 10
      mul     ebx
      mov     ebx, eax
      dec     edi                 ; reset pointer
    jmp     @Next
    @Exit:
    mov     eax, esi
    @NoFound:
    ret
  str2num endp










  start:

   
    ; Get the numbers in asciiz
    invoke     StdOut, offset szTxtNum1
    invoke     StdIn, offset szNumber1, cdMaxSize
    invoke     StdOut, offset szTxtNum2
    invoke     StdIn, offset szNumber2, cdMaxSize
   
    ; Convert to numbers and sum
    mov        eax, offset szNumber1
    call       str2num
    mov        ebx, eax
   
    mov        eax, offset szNumber2
    call       str2num
    add        ebx, eax
   
    ; Show the result
    invoke     StdOut, offset szTxtSum
    mov        eax, ebx
    call       @num2str

    invoke     StdOut, eax
    inkey
    INVOKE     ExitProcess, 0
   


   
   

END start

RuiLoureiro

 :biggrin:
Hi
    I think you need to start learning assembly.
    Start with a very simple code.
    Think about something simple to console assemble and link and start to post...
Good luck
     

EmuRatti39

Thanks for the reply RuiLoureiro ,

I forgot to start with a small program   :biggrin:

jj2007

The whole code? No, but mark 10 lines with ; ??, and I am willing to invest some time.

hutch--

This will build as a console app. Problems were,

1. No prototypes.
2. non volatile register EBX not preserved. I substituted ESI and preserved it.

    INCLUDE    \masm32\include\masm32rt.inc

    cdMaxSize  EQU 11

     @num2str PROTO
     str2num  PROTO

    .DATA?
      szNumber1 db cdMaxSize dup(?)
      szNumber2 db cdMaxSize dup(?)
      iNum      dd ?

    .DATA
      szTxtNum1 db "Your first number: ", 0
      szTxtNum2 db "Your second number: ", 0
      szTxtSum  db "Both sum: ", 0
      number1   DD 442
      number2   dd 22
      msg       db cdMaxSize dup(0)

.CODE

  start:

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    ; Get the numbers in asciiz
    invoke     StdOut, offset szTxtNum1
    invoke     StdIn, offset szNumber1, cdMaxSize
    invoke     StdOut, offset szTxtNum2
    invoke     StdIn, offset szNumber2, cdMaxSize
   
    ; Convert to numbers and sum

    push esi   ; ADDED

    mov        eax, offset szNumber1
    call       str2num
    mov        esi, eax
   
    mov        eax, offset szNumber2
    call       str2num
    add        esi, eax
   
    ; Show the result
    invoke     StdOut, offset szTxtSum
    mov        eax, esi
    call       @num2str

    invoke     StdOut, eax
    invoke     StdOut,"Bye"
    invoke     StdOut,chr$(13,10,13,10)

    inkey

    pop esi   ; ADDED

    INVOKE     ExitProcess, 0

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

  @num2str  proc uses ebx edx esi
    ; In: eax, number value
    ; Out: eax, offset of value in string format
    mov     esi, offset msg + cdMaxSize - 1
    mov     ebx, 10
    @Next:
      dec     esi
      xor     edx, edx
      div     ebx
      or      edx, 30h
      mov     byte ptr [esi], dl
      or      eax, eax
    jnz     @Next
    mov     eax, esi
    ret
  @num2str endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

  str2num proc uses ebx ecx edx esi edi
    ; In: eax, offset to str_num
    ; out: eax, the number converted
    mov     edi, eax              ; offset
    mov     eax, 0
    mov     ecx, cdMaxSize
    repne   scasb
    jne     @NoFound
    sub     edi, 2
    sub     ecx, cdMaxSize
    neg     ecx
    mov     ebx, 1                ; factor
    mov     esi, 0                ; Acumulative
    @Next:
      dec     ecx
      jl      @Exit
      xor     eax, eax            ; clear it
      mov     al, byte ptr [edi]
      and     al, 15              ; 2num
      mul     ebx
      add     esi, eax            ; get into accumulative
      mov     eax, ebx            ; increases factor
      mov     ebx, 10
      mul     ebx
      mov     ebx, eax
      dec     edi                 ; reset pointer
    jmp     @Next
    @Exit:
    mov     eax, esi
    @NoFound:
    ret
  str2num endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

END start

hutch--

Here is how you would normally write a test piece in MASM32.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    LOCAL pbuf  :DWORD
    LOCAL buff[64]:BYTE
    LOCAL var1  :DWORD
    LOCAL var2  :DWORD

    lea eax, buff
    mov pbuf, eax                           ; get the buffer address

    print "Enter only numbers",13,10,13,10

    print "Enter your first number",13,10
    invoke StdIn,pbuf,64
    mov var1, uval(pbuf)                    ; unsigned conversion

    print "Enter your second number",13,10
    invoke StdIn,pbuf,64
    mov var2, uval(pbuf)                    ; unsigned conversion

    mov eax, var2                           ; copy var2 to eax
    add var1, eax                           ; add eax to var1

    print "The sum of the two numbers = "
    print str$(var1),13,10,13,10            ; display result in var1

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start

EmuRatti39

Thanks for the reply jj2007 , hutch--


:biggrin:

avcaballero

Thais is a code i posted here some days ago. What do you want todo ask?

EmuRatti39

caballero ,

Half of it i already figured it out by looking at many examples . I will try to understand the remaining when i  can find a  little bit more time

Thanks  :biggrin:

hutch--

Get rid of this at the start of your code.

.386
.MODEL     flat, stdcall
OPTION     casemap :none

It is already done in masm32rt.inc.