News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Please help with this code , Second try at assembly :)

Started by Tortoiseman39, April 26, 2019, 11:29:49 PM

Previous topic - Next topic

Tortoiseman39

I have been trying to learn Assembly language for a while now .
Many Assemblers many syntax's and finally i think i am getting comfortable with the syntax of masm32

Other assemblers i have tried to learn is lot more complicated to understand .

I do not like to go through 2 or 3 assemblers simply to learn the basics of the language .

So i am strictly trying to stay with masm32 .

What does this line means ?

include \masm32\include\masm32rt.inc

.data?                  ;This contains uninitialized variables
somevar dd ?

.data                    ;This contains initialized variables
txHello db "Hello World", 0

.code                    ;This is where your code resides
start:
  inkey offset txHello
  exit
end start



Quoteinkey offset txHello



jj2007

inkey is a macro of the Masm32 SDK (which you load with masm32rt.inc). The syntax is identical to the print macro, but it waits for a keypress.

You can find the documentation of inkey, print, etc in C:\Masm32\help\hlhelp.chm under Macro Categories/Console Mode Macros
The file, unfortunately, is not searchable; there is an extended help system called xHelp that allows to search for all kinds of keywords.

Tortoiseman39

Thanks jj2007 ,

For the purpose of understanding and learning  i have been trying to see the assembly instructions like this ,

Opcode , Operand , Data

What i wanted to say was that ,

Is it ok to see the keyword inkey as a sort of Opcode ? and offset is an operand ?

QuoteThe syntax is identical to the print macro, but it waits for a keypress.


QuoteYou can find the documentation of inkey, print, etc in C:\Masm32\help\hlhelp.chm under Macro Categories/Console Mode Macros
The file, unfortunately, is not searchable; there is an extended help system called xHelp that allows to search for all kinds of keywords.

Wow , Thanks

I will try to search like that

jj2007

Quote from: Tortoiseman39 on April 27, 2019, 12:38:51 AMIs it ok to see the keyword inkey as a sort of Opcode ? and offset is an operand ?

A line like inkey offset txHello looks like this "under the hood":
machine code      assembly opcodes
68 00804000       push offset txHello                  ; Arg1 = ASCII "Hello World"
E8 65000000       call StdOut
E8 9C000000       call wait_key
68 59804000       push offset ??00B3                   ; Arg1 = ASCII CR,LF
E8 56000000       call StdOut


An argument (offset txHello) is pushed on the stack, and then a function is being called. You can study the functions in \Masm32\m32lib

Tortoiseman39

@jj2007 ,

Thanks for the reply .

I did a few things , i am not sure if i am allowed to do this .

But i converted every chm file in  C:\Masm32\help\ into a pdf file

Now i can search easily too

I hope this is an alright thing to do

hutch--

Just don't try and distribute them, the content is copyright software.

Tortoiseman39


Tortoiseman39

I have one more question ,

What is the best thing to call this line  ,



.data                    ;This contains initialized variables
txHello db "Hello World", 0


Initialized data called txHello  ?
Initialized variable called txHello  ?


I think this is more correct , right ?




include \masm32\include\masm32rt.inc

.data?                 
uninitializeddataname  dd ?                                                               ;This contains uninitialized data

.data                   
initializeddataname db "Hello World", 0                                      ;This contains initialized data

.code                                                                            ;This is where your code resides
start:
  inkey offset initializeddataname
  exit
end start


Tortoiseman39

I want to understand this code , after i understand the above properly




.386
.MODEL     flat, stdcall
OPTION     casemap :none



INCLUDE    \masm32\include\masm32rt.inc



cdMaxSize  EQU 11

.DATA?                                                                         ;This contains uninitialized data
  szNumber1 db cdMaxSize dup(?)
  szNumber2 db cdMaxSize dup(?)
  iNum      dd ?




.DATA                                                                           ;This contains initialized 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                                                                         ;This is where your code resides
  @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

jj2007

Quote from: Tortoiseman39 on April 27, 2019, 05:17:46 AM
I want to understand this code , after i understand the above properly

Study the code, try to understand, and put a comment behind each (or almost each) line. Then post it here, and we'll tell you if the comments are correct.

Tortoiseman39

@jj2007

Yes , i will do that

I am happy that i was able to organize at least this much .

This now can be learned a lot more easily than the old times .

Thanks for the reply