News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Just a couple of question about this code

Started by bluew, August 18, 2011, 05:17:15 PM

Previous topic - Next topic

bluew

Good Day,

My name is Roberto from Italy, this is my first time here
Im reading tutorial about Asm ( author JEFF HUANG ) http://www.acm.uiuc.edu/sigwin/old/workshops/winasmtut.pdf
I would like learning assembly without miss any detail, so I ask you a couple of questions :)
Im talking about MASM32 assembler on Intel arch. , this is the code :)
Con you help me to figure out  ? There are some comments made from 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
    ProgramText db      "Hello World!", 0
    BadText     db      "Error: Sum is incorrect value", 0
    GoodText    db      "Excellent! Sum is 6", 0
    Sum         sdword  0
.code
start:
                     ;                                  eax
        mov ecx, 6   ; set the counter to 6                  ecx = 6
        xor eax, eax ; set eax to 0                              eax = 0
_label: add eax, ecx ; add the numbers                     eax = eax + ecx -> eax = eax +6 -> eax = eax +5 ->  eax = eax +4-> and soon on till to zero
        dec ecx      ;     from 0 to 6                            ecx--
        jnz _label   ;  ----------------------------                    if ecx !=0 goto label
        mov edx, 7   ;                                               eax = 21          edx = 7
        mul edx      ; multiply by 7                              multiply by 7 , who ?(maybe eax ? )         
        push eax     ; pushes eax into the stack           Why do you need push/pops here ?Cant I move eax to sum straight ?
        pop Sum      ; pops eax and places it in Sum
        cmp Sum, 147 ; compares Sum to 147            Sum = Sum -147
        jz _good     ; if they are equal, go to _good     if zero = true goto _good
_bad:   invoke StdOut, addr BadText
        jmp _quit
_good:  invoke StdOut, addr GoodText
_quit:  invoke ExitProcess, 0
end start

Thanks for your time

jj2007

Ciao Roberto,

It seems your only question is why push eax, pop Sum. No idea - ask the author :bg
mov Sum, eax would also work. However, mov Sum1, Sum2 (for example) would not work, as memory to memory is not possible (at least not with the standard opcodes).

Some tips to get you started are below in my signature. And don't forget to install Olly, it's a must to learn assembler.

Benvenuto al Forum :thumbu

bluew

Grazie seguirò i tuoi consigli :) anche se non sarà cosa facile

Thanks for the advice :)  I will try to learn asm but I know it will not so easy..


Thanks for your time

dedndave

welcome to the forum, Roberto   :U
veni vidi vici
that's all the "Italiano" i can remember   :P

lingo

#4
American stultitia .. :lol

Latin: veni, vidi, vici
Italiano: Sono venuto, ho visto, ho conquistato
English: I came, I saw, I conquered
German: Ich kam, ich sah, ich eroberte
French: Je suis venu, j'ai vu j'ai vaincu

dedndave

oh really ?
you'd think i'd know that after taking all those years of Latin   ::)

jj2007

Quote from: lingo on August 18, 2011, 07:38:45 PM
American stultitia .. :lol

Latin: veni, vidi, vici
Italiano: Sono venuto, ha visto, ha conquistato
English: I came, saw, conquered

Italiano: Sono venuto, haho visto, haho conquistato

(the English version seems correct as far as I can see ::))

bluew

Giulio ! :D

I Installed Ollydbg.It let me watch some stuff that I cant see on my code, so I think it's a bit hard at moment :)
may be MASM hidden some piece of code by Macro ( for example invoke ) I dont know :)
I'am reading the great tutorials of Iczelion :
because the macro, the code look like high level language so I'm already thinking  on what application focus my efforts :)

sry for bad english.
Thanks for your time

dedndave

maybe we can get Jochen to write a nice Olly crash-course tutorial   :bg

Magnum

bluew,

Another excellent tool is IDA Pro 5.0.

It lets you make an .asm listing of any Windows program.

http://www.hex-rays.com/idapro/idadown.htm
Have a great day,
                         Andy

bluew

Thanks but I started with asm  some days ago.. :dazzled:

My goal is create an application in order to  read data from USB and draw graphic on monitor.................


Thanks for your time

jj2007

Quote from: bluew on August 18, 2011, 09:21:00 PM
Giulio ! :D

I Installed Ollydbg.It let me watch some stuff that I cant see on my code, so I think it's a bit hard at moment :)
may be MASM hidden some piece of code by Macro ( for example invoke ) I dont know :)


invoke is a macro that pushes parameter in the right order (right first) and checks their number and type.
Other macros like print, MsgBox etc make your life easier but Olly reveals that under the hood many things are going on.
Below an example. The nops n (a macro...) are to make reading easier.


include \masm32\include\masm32rt.inc

.code
AppName db "Masm32:", 0
hwText db "Hello World #2", 0

start: nops 2
invoke MessageBox, 0, addr hwText, addr AppName, MB_OK
nops 2
MsgBox 0, "Hello World", addr AppName, MB_OK
nops 2
push MB_OK
push offset AppName
push offset hwText
push 0
call MessageBox
nops 2
exit 123h ; invoke ExitProcess, 123h

end start


CPU Disasm
Address    Hex dump          Command                      Comments
AppName     .  4D 61 73 6D 3 ascii "Masm32:",0            ; ASCII "Masm32:"
hwText      .  48 65 6C 6C 6 ascii "Hello World #2",0     ; ASCII "Hello World #2"
<ModuleEnt  .  90            nop
00401018    .  90            nop
00401019   Ú.  6A 00         push 0                       ; ÚType = MB_OK|MB_DEFBUTTON1|MB_APPLMODAL
0040101B   ³.  68 00104000   push AppName                 ; ³Caption = "Masm32:"
00401020   ³.  68 08104000   push hwText                  ; ³Text = "Hello World #2"
00401025   ³.  6A 00         push 0                       ; ³hOwner = NULL
00401027   ³.  E8 4A000000   call MessageBoxA             ; ÀUSER32.MessageBoxA
0040102C   ³.  90            nop
0040102D   ³.  90            nop
0040102E   ³.  6A 00         push 0                       ; ÚType = MB_OK|MB_DEFBUTTON1|MB_APPLMODAL
00401030   ³.  68 00104000   push AppName                 ; ³Caption = "Masm32:"
00401035   ³.  68 00404000   push offset ??0019           ; ³Text = "Hello World"
0040103A   ³.  6A 00         push 0                       ; ³hOwner = NULL
0040103C   ³.  E8 35000000   call MessageBoxA             ; ÀUSER32.MessageBoxA
00401041   ³.  90            nop
00401042   ³.  90            nop
00401043   ³.  6A 00         push 0                       ; ÚType = MB_OK|MB_DEFBUTTON1|MB_APPLMODAL
00401045   ³.  68 00104000   push AppName                 ; ³Caption = "Masm32:"
0040104A   ³.  68 08104000   push hwText                  ; ³Text = "Hello World #2"
0040104F   ³.  6A 00         push 0                       ; ³hOwner = NULL
00401051   ³.  E8 20000000   call MessageBoxA             ; ÀUSER32.MessageBoxA
00401056   ³.  90            nop
00401057   ³.  90            nop
00401058   ³.  6A 00         push 0                       ; ÚExitCode = 0
0040105A   À.  E8 1D000000   call ExitProcess             ; ÀKERNEL32.ExitProcess

FORTRANS

Quote from: bluew on August 18, 2011, 09:21:00 PM
may be MASM hidden some piece of code by Macro ( for example invoke ) I dont know :)

Hi,

   If you are really worried about the exact code generated,
all you need to do is have MASM generate a listing.  That
will show every byte generated.  You should exclude the
include files with the following:


        .XCREF
        .XLIST
INCLUDE  {the include files}
        .LIST
        .CREF


Regards,

Steve N.

MichaelW

Quote from: jj2007 on August 18, 2011, 11:31:27 PM
invoke is a macro that pushes parameter in the right order (right first) and checks their number and type.

Right first is the norm, but it actually depends on what is specified in the langtype field of the MODEL, PROTO, or PROC directives.

eschew obfuscation

jj2007

Quote from: FORTRANS on August 19, 2011, 11:49:14 AM
If you are really worried about the exact code generated,
all you need to do is have MASM generate a listing.

Here is the listing matching the code above, with Steve's settings. Personally I prefer Olly's disassembly, but it's a matter of taste.

Microsoft (R) Macro Assembler Version 6.15.8803     08/19/11 17:40:23
tmp_file.asm      Page 1 - 1


        .XCREF
        .XLIST
        .LIST

00000000 .code
00000000 4D 61 73 6D 33 AppName db "Masm32:", 0
   32 3A 00
00000008 48 65 6C 6C 6F hwText db "Hello World #2", 0
   20 57 6F 72 6C
   64 20 23 32 00

00000017 start: nops 2
00000017  90      2         nop
00000018  90      2         nop
invoke MessageBox, 0, addr hwText, addr AppName, MB_OK
nops 2
0000002C  90      2         nop
0000002D  90      2         nop
MsgBox 0, "Hello World", addr AppName, MB_OK
00000000      2         .data
00000000 48 65 6C 6C 6F     2           ??0019 db "Hello World",0       
   20 57 6F 72 6C
   64 00
0000002E      2         .code
nops 2
00000041  90      2         nop
00000042  90      2         nop
00000043  6A 00 push MB_OK
00000045  68 00000000 R push offset AppName
0000004A  68 00000008 R push offset hwText
0000004F  6A 00 push 0
00000051  E8 00000000 E call MessageBox
nops 2
00000056  90      2         nop
00000057  90      2         nop
exit 123h ; invoke ExitProcess, 123h

end start