Author Topic: Please help with this code , Second try at assembly :)  (Read 689 times)

Tortoiseman39

  • Regular Member
  • *
  • Posts: 7
Please help with this code , Second try at assembly :)
« on: April 26, 2019, 11:29:49 PM »
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 ?

Code: [Select]
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


Quote
inkey offset txHello



jj2007

  • Member
  • *****
  • Posts: 9743
  • Assembler is fun ;-)
    • MasmBasic
Re: Please help with this code , Second try at assembly :)
« Reply #1 on: April 27, 2019, 12:28:25 AM »
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

  • Regular Member
  • *
  • Posts: 7
Re: Please help with this code , Second try at assembly :)
« Reply #2 on: April 27, 2019, 12:38:51 AM »
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 ?

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


Quote
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.

Wow , Thanks

I will try to search like that

jj2007

  • Member
  • *****
  • Posts: 9743
  • Assembler is fun ;-)
    • MasmBasic
Re: Please help with this code , Second try at assembly :)
« Reply #3 on: April 27, 2019, 01:00:54 AM »
Is 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":
Code: [Select]
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

  • Regular Member
  • *
  • Posts: 7
Re: Please help with this code , Second try at assembly :)
« Reply #4 on: April 27, 2019, 02:06:00 AM »
@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--

  • Administrator
  • Member
  • ******
  • Posts: 6715
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Please help with this code , Second try at assembly :)
« Reply #5 on: April 27, 2019, 03:21:27 AM »
Just don't try and distribute them, the content is copyright software.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

Tortoiseman39

  • Regular Member
  • *
  • Posts: 7
Re: Please help with this code , Second try at assembly :)
« Reply #6 on: April 27, 2019, 04:19:14 AM »
@hutch ,

It will stay in my pc only  :biggrin:

Tortoiseman39

  • Regular Member
  • *
  • Posts: 7
Re: Please help with this code , Second try at assembly :)
« Reply #7 on: April 27, 2019, 04:41:42 AM »
I have one more question ,

What is the best thing to call this line  ,

Code: [Select]

.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 ?


Code: [Select]

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

  • Regular Member
  • *
  • Posts: 7
Re: Please help with this code , Second try at assembly :)
« Reply #8 on: April 27, 2019, 05:17:46 AM »
I want to understand this code , after i understand the above properly

Code: [Select]


.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

  • Member
  • *****
  • Posts: 9743
  • Assembler is fun ;-)
    • MasmBasic
Re: Please help with this code , Second try at assembly :)
« Reply #9 on: April 27, 2019, 05:39:39 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

  • Regular Member
  • *
  • Posts: 7
Re: Please help with this code , Second try at assembly :)
« Reply #10 on: April 27, 2019, 06:03:01 AM »
@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