News:

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

Intimidating Asm

Started by hello, December 12, 2008, 05:10:51 AM

Previous topic - Next topic

hello

This is the source in c:
#include<stdio.h>
int main ()
{
printf ("GOOD LUCK");
return 0;
}
---------------------------
The output of the above code in asm, using bcc32 and -S switch is as:
   .386p
ifdef ??version
if    ??version GT 500H
.mmx
endif
endif
model flat
ifndef ??version
?debug macro
endm
endif
?debug S "test.c"
?debug T "test.c"
_TEXT segment dword public use32 'CODE'
_TEXT ends
_DATA segment dword public use32 'DATA'
_DATA ends
_BSS segment dword public use32 'BSS'
_BSS ends
DGROUP group _BSS,_DATA
_TEXT segment dword public use32 'CODE'
_main proc near
?live1@0:
   ;
   ; int main()
   ;
push      ebp
mov       ebp,esp
   ;
   ; {
   ; printf("");
   ;
@1:
push      offset s@
call      _printf
pop       ecx
   ;
   ; return 0;
   ;
xor       eax,eax
   ;
   ; }
   ;
@3:
@2:
pop       ebp
ret
_main endp
_TEXT ends
_DATA segment dword public use32 'DATA'
s@ label byte
db 0
align 4
_DATA ends
_TEXT segment dword public use32 'CODE'
_TEXT ends
public _main
extrn   _printf:near
?debug D "c:\Borland\Bcc55\include\_nfile.h" 10459 10272
?debug D "c:\Borland\Bcc55\include\_null.h" 10459 10272
?debug D "c:\Borland\Bcc55\include\_defs.h" 10459 10272
?debug D "c:\Borland\Bcc55\include\_stddef.h" 10459 10272
?debug D "c:\Borland\Bcc55\include\stdio.h" 10459 10272
?debug D "test.c" 14732 19942
end
--------------------
And our good old simple lines of code in asm which practically performs the same function is:
DATA SECTION
KEEP  DD 0             
;
CODE SECTION
START:
PUSH -11               
CALL GetStdHandle     
PUSH 0, ADDR KEEP       
PUSH 9
PUSH "GOOD LUCK "
PUSH EAX               
CALL WriteFile
;
XOR EAX, EAX             
RET
--------------------
What is the difference between these two representations other than that one looks very simple while the other is so intimidating for a newcomer like me ? and why it is so ?

Regards...

jj2007

Quote from: hello on December 12, 2008, 05:10:51 AM
And our good old simple lines of code in asm which practically performs the same function is:
DATA SECTION
KEEP  DD 0             
;
CODE SECTION
START:
PUSH -11               
CALL GetStdHandle     
PUSH 0, ADDR KEEP       
PUSH 9
PUSH "GOOD LUCK "
PUSH EAX               
CALL WriteFile
;
XOR EAX, EAX             
RET
--------------------


Oh my god, what is that horribly complicated code doing??
Try this, it's fully sufficient:
include \masm32\include\masm32rt.inc

.code
start: print "Good luck"
exit

end start

Vortex

jj, hello is using GoAsm. The StdOut function from masm32.lib uses the same GetStdHandle & WriteFile APIs to output strings.

Vortex

Hi Hello,

You can create a similar application with GoAsm :

CODE

start:

    invoke  printf,"Good luck"
    add     esp,4 ; printf took only one parameter here
    ret


To build it :

\goasm\goasm Test.asm
\goasm\golink /console Test.obj msvcrt.dll


jorgon

Hello Hello

The disassembly includes a lot of setting-up (which is largely unnecessary in assembler and is reduced to a minimum in GoAsm) and it also includes the original source code.  So it all appears very complex.  Pared down the disassembly is:-

push      ebp
mov       ebp,esp
push      offset s@
call      _printf
pop       ecx
xor       eax,eax
pop       ebp
ret


A few points about the above.  The push ebp, mov ebp,esp and pop ebp instructions create a stack frame which permits you to use local data.  Local data is data which can be read within the stack frame only, as opposed to data which every part of your program can read.  In fact a stack frame is unnecessary in this little program.  I only use stack frames in windows procedures, callbacks and in recursive or multi-threaded programs.  The "push offset s@" provides the address of the "good luck" string although the disassembly does not show this completely.

The "C" source uses printf which is part of the C run-time library.  As Vortex points out this can also be used in assembler - it is contained within msvcrt.dll
You have to be careful using it in assembler however, since it does not restore the stack position.  So this differs from almost all functions you will call when writing Windows applications.  Because of this, you see in the disassembly the line "pop ecx" which does this job for you (this adds 4 bytes to the esp register and therefore restores the stack by one dword - the push sending the parameter to the function being a dword). 

So, if you write this little program in assembler without the unnecessary baggage you end up with:-


Code
Start:
push   addr 'Good Luck'
call     printf
pop       ecx
xor       eax,eax
ret


The xor eax,eax simply returns code FALSE to Windows after you run the program to show that the program ran without errors (this is the same as the "C" return 0).
You must include /console in the GoLink command line, printf will not work without it.  This creates an executable which is marked as a console program as opposed to a program which has a message queue (and a window).
Author of the "Go" tools (GoAsm, GoLink, GoRC, GoBug)

hello

jj2007...
Thank you.
Oh my god, what is that horribly complicated code doing??
To tell you the truth, I feel the same way as you do in this matter. But that is the way bcc32 output source in C to assembly. They must have well-built reason to give out that kind of output; I stand no chance even to muse over that area.

Try this, it's fully sufficient:jj2007...
Of course, I can see that the Code you have given looks very simple and easy to understand. But, my modest understanding in this area is confined to GoAsm. I feel comfortable with GoAsm. Vortex has filled in with more details in this matter. These guys are crazy here just doing everything to make the going on easier, informative and instructive.

Thank you once again for your participation....



askm


Presumably this team stood on the shoulders of previous teams.

By now the team IS intimidating.

But they only wanted to change a light bulb.

Changing a light bulb can be simpler.

In bcc32's case...

hello

Vortex...

CODE
start:
    invoke  printf,"Good luck"
    add     esp,4 ; printf took only one parameter here
    ret

To build it :
Code:
\goasm\goasm Test.asm
\goasm\golink /console Test.obj msvcrt.dll

yes...it works fine but this time i needed to please msvcrt.dll instead of kernel32.dll !!
Thank you..

Vortex

Hi hello,

msvcrt.dll is not a problem. It comes with Windows 2000, Xp and Vista installations ( plus Windows Server 2000/2003/2008 versions )

Microsoft's explanation :

QuoteThe msvcrt.dll is now a "known DLL," meaning that it is a system component owned and built by Windows. It is intended for future use only by system-level components.

http://msdn.microsoft.com/en-us/library/abx4dbyh.aspx

C:\>dir \WINDOWS\system32\msvcrt.dll

At the command-prompt , this directive should return you the output below ( tested on a Xp Sp 3 system )

14.04.2008  08:00           343.040 msvcrt.dll



hello

#9
jorgon ...

   1- Code:   Local data is data which can be read within the stack frame only
   Q. By local data, i suppose you mean local variable . Aren't you? i suppose both are one and the same!!

   2- Code:   In fact a stack frame is unnecessary in this little program.
   Q. As a reversal to this statement , if you were to use stack frame in this small program, what would be the code structure look like?

Code
Start:
push   addr Good Luck
call     printf
pop     ecx
xor      eax,eax
ret

   3- Code:   The "push offset s@" provides the address of the "good luck" string although
    the disassembly does not show this completely.

   Q. if it is not very tiring, a modest illumination on this above account would be very helpful.

Regards....

Vortex

With the stack frame, the code should look like the following :

data

string  db 'Good luck',0

code

start:

    call    main
    invoke  ExitProcess,eax

main:

    push    ebp
    mov     ebp,esp
    push    ADDR string
    call    printf
    pop     ecx     ; or add esp,4
    xor     eax,eax
    pop     ebp
    ret


Local data = local variable = data in the stack frame

jorgon

Hello Hello

The only remaining question is:-

Quote
The "push offset s@" provides the address of the "good luck" string although the disassembly does not show this completely.   Q. if it is not very tiring, a modest illumination on this above account would be very helpful.

Push offset s@ is the same as Push Addr s@ and pushes onto the stack the address of a label called "s@".

In your disassembly of the "C" version there is this code fragment:-

_DATA             segment dword public use32 'DATA'
s@            label          byte
                db             0
                align           4
_DATA      ends


This shows that a data section is opened and a data label called "s@" is made and a byte of data is declared at the address of the label.
However I do not think this is an accurate and full disassembly, since we know that in fact the string "GOOD LUCK" is declared at the label s@.
I believe a more accurate disassembly would have been:-

_DATA             segment dword public use32 'DATA'
s@            label          byte
                db             "GOOD LUCK",0
                align           4
_DATA      ends


Author of the "Go" tools (GoAsm, GoLink, GoRC, GoBug)

hello

Excellent...Vortex ..

Earlier, a number of code snippets were used in isolation to explain ideas like stack , call and so on.
i was wondering how to put all these pieces together...Well...Here it is... starting the code with (DATA) which i was looking for(as my beginning was with the code from GoAsm manual)...This illustration (code) by Vortex is like custom made to suite me. i am beginning to get an image ; full profile of the code i need to learn in coming days.

'Crawling on all fours, it self is a milestone' ; now i must be able to take a step forward.

Before that allow me to thank all those whose contribution, suggestions and participation that gave me encouragement and strength to reach this 'pushing up from the floor' stage. In this context i remember  jj2007, askm , Mark jones, donkey  and last but not least,
Vortex ; the embodiment of serenity.

Thank you ...all of you ...and 2 you too GoAsm (or jorgon ).