C++ Learning Community Forum
August 01, 2010, 02:49:07 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: 1 [2]
  Print  
Author Topic: ASM tut.  (Read 4506 times)
C-Man
Does anyone even read this ?
Global Moderator
Dr. of C++ology
*****
Posts: 988



View Profile WWW
« Reply #15 on: April 13, 2007, 02:11:47 PM »

on linux it's int 0x80 and oyu can still link to libc or any of the posix libs wihtoput problem and have all the os api without using int's
Logged

FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #16 on: April 14, 2007, 01:56:34 AM »

Shonoby asked for a Hello World program well here is one. i commented it heavily and used some macros which i don't really like just to make it easier to read for our C and C++ programmers.

I created and tested this using FASM (http://flatassembler.net/)
Code:
FORMAT PE GUI 4.0
;this is FASM specific it's a command that specify's the basic format of the file
;while it's not mandatory your os wont know how to read the direct code without it

ENTRY  Main  ;entry point Main
;this specify's the location to start this can be a function location or a
;regular location just dont specify a location in your data section it will
;most likely cause a crash

include 'win32a.inc'
;same as in C except that FASM uses the .inc extension.
;win32a.inc specify's the basic macros and structs used.

section '.data' data readable writeable
;specify's that we want to use the .code section and that it contains...
;well... Data. it also specifys that this section has read (readable)
;and Write (writeable) privileges


;to save space as each section in a PE file takes 1kb i will also use this
;section to store constants

hInstance       dd  ?

szNotice        db  'Notice', 0  ;you need to add the ',0' to the end of strings in asm
                                 ;to add the null terminator.
szHello         db  'Hello World of ASM', 0

;this is how you define a variable.
;<var name> <type> <initial data (if '?' then data is not initialized>
;dd is Data DoubleWord a 4 byte data type (like int on 32 bit C compilers)
;dw is Data Word a 2 byte data type (like a short)
;db is Dara Byte a 1 byte data type (like a char)
;   db has some special properties that allow you to use it like a string

;you can chain data declarations by separating them with a comma
;EX: array      dd 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
;will is the same as "int array[10]" and filling it with sequential variables from 0 to 9


section '.code' code readable executable
;specify's that we want to use the .code section and that it contains Code.
;it also specify's that this section has read (readable)
;and Ecexute (executable) privileges

proc  Main
;I usually prefer the old style calls in ASM but i used the invoke syntax here
;to make your port from C to asm easier
  invoke  GetModuleHandle, 0 ;this is the same as "GetModuleHandle(NULL);" in c
  mov     [hInstance], eax   ;most api save their returns in eax this command moves
                             ;the return from GetModuleHandle into the variable hInstance
                             ;notice the '[ ]' around the variable if thesse were here
                             ;FASM would assume that hInstance was a constant address.
                             ;and would return an error. the '[ ]' tell FASM that
                             ;hInstance is an Data Location. which allows us to save
                             ;our Handle to this application insatance.

  invoke  MessageBox, 0, szHello, szNotice, MB_OK
  ;this calls the MessageBox function (specifyed later in the imports section)
  ;then passes it some parameters. The first parameter which tells MessageBox
  ;which window owns our messagebox in this case it's a 0 or NULL which tells
  ;MessageBox that this message box isn't owned by a window and to assign it
  ;to the desktop. the second paramiter is our message, the third is the
  ;messagebox caption (or window name), and the last is the type of messagebox.

  ret
  ;last we need to return control to the OS, there are 2 ways to do this one is
  ;to call ExitProcess the other is to use a 'ret' or return.
  ;(ret only works if you kept your stack balanced properly)
endp
; last is endp, endp is required to tell fasm when the function has ended

section '.idata' import data readable writeable
;this is a special section. the infamous Imports Table used by many windows hackers.
;I'll need to give you a little background for you to understand it completely.
;Every program needs to link to the outside OS. Well the the Imports table is how
;a windows based program does that. When the program first starts the OS scand
;the program for where the imports table is located. after the imports table is found
;the OS looks for all functions it recognizes if it finds one it doesn't know then it
;reports an error and exits the program. if no errors are found then the OS places the
;address of all functions found into the locations specified in the imports table
;structures. This usually all happens transparent to you the programmer (especially in
;C based languages). all you really need to know is that unlike in C and C++ you need to
;tell FASM which API you need.
;(this is not specified in win32a.inc, so win32a.inc != windows.h)

  library kernel, 'KERNEL32.DLL',\
          user,   'USER32.DLL'

;the "library" macro (specified in win32a.inc) tells FASM to create a list of DLL's
;to link to and assign those DLL's a variable name. this macro use a pair sequence first
;the variable name then the library's file name. the ',' separates the parameters (just
;like in c), and the '\' tells FASM to carry the macro/instruction over to the next line.
;in this case were using Kernel32.dll (no case doesn't matter but if you match the case
;of the dll it can help avoid errors) and User32.dll.

  import  kernel,\
          GetModuleHandle,'GetModuleHandleA',\
          ExitProcess,    'ExitProcess'
;the "import" macro (specified in win32a.inc) specify's the format of the imported functions
;in this case we are importing GetModuleHandleA and ExitProcess from kernel (KERNEL32.DLL)
;(Note: some API have two versions standard ASCII and Wide Char. these are specified by the
;'A' and the 'W' on the end of the function.

  import  user,\
          MessageBox,     'MessageBoxA'
« Last Edit: April 14, 2007, 02:04:18 AM by FrozenKnight » Logged


Imagine the impossible, then make it happen.
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #17 on: April 21, 2007, 01:59:41 AM »

in the previous example i gave you an "dumbed down" asm program. this isn't true asm. it isn't even close to what you would see in a debugger window. so in this example i will modify the previous source and show you some real asm.

I created and tested this using FASM (http://flatassembler.net/)
Code:
FORMAT PE GUI 4.0
;this is FASM specific it's a command that specify's the basic format of the file
;while it's not mandatory your os wont know how to read the direct code without it

ENTRY  Main  ;entry point Main
;this specify's the location to start this can be a function location or a
;regular location just dont specify a location in your data section it will
;most likely cause a crash

include 'win32a.inc'
;same as in C except that FASM uses the .inc extension.
;win32a.inc specify's the basic macros and structs used.

section '.data' data readable writeable
;specify's that we want to use the .code section and that it contains...
;well... Data. it also specifys that this section has read (readable)
;and Write (writeable) privileges


;to save space as each section in a PE file takes 1kb i will also use this
;section to store constants

hInstance       dd  ?

szNotice        db  'Notice', 0  ;you need to add the ',0' to the end of strings in asm
                                 ;to add the null terminator.
szHello         db  'Hello World of ASM', 0

;this is how you define a variable.
;<var name> <type> <initial data (if '?' then data is not initialized>
;dd is Data DoubleWord a 4 byte data type (like int on 32 bit C compilers)
;dw is Data Word a 2 byte data type (like a short)
;db is Dara Byte a 1 byte data type (like a char)
;   db has some special properties that allow you to use it like a string

;you can chain data declarations by separating them with a comma
;EX: array      dd 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
;will is the same as "int array[10]" and filling it with sequential variables from 0 to 9


section '.code' code readable executable
;specify's that we want to use the .code section and that it contains Code.
;it also specify's that this section has read (readable)
;and Ecexute (executable) privileges

;ok this is where the real asm comes in as proc is the first non ams instruction i included.
;insted of proc we will just place a label with the same name
;then we would initialize the stack by pushing esp onto the stack then copying esp to ebp
;in this application we don't use the stack enough to warrant such use so i will leave commented versions here

;  proc  Main
Main:
;uncomment the flowing 2 lines if you use need to use ebp offsets with local variables
;or just use the standard proc function prototype
;  push    esp
;  mov     ebp, esp

;Ok i hate invoke it hides too much and it doesnt allow some of the advanced nature of asm to be seen
;in asm you can modify paramiters at any point invoke even recalculate them after you placed them on the stack
;    invoke  GetModuleHandle, 0 ;this is the same as "GetModuleHandle(NULL);" in c

  push    0   ;push the paramiter 0 onto the stack
  call    [GetModuleHandle]  ;call the API
  mov     [hInstance], eax   ;most api save their returns in eax this command moves
                             ;the return from GetModuleHandle into the variable hInstance
                             ;notice the '[ ]' around the variable if thesse were here
                             ;FASM would assume that hInstance was a constant address.
                             ;and would return an error. the '[ ]' tell FASM that
                             ;hInstance is an Data Location. which allows us to save
                             ;our Handle to this application insatance.

  ;invoke  MessageBox, 0, szHello, szNotice, MB_OK

  push    MB_OK    ;ok here is where it first becomes apparent that parameters are pushed onto the stack in reverse order
  push    szNotice  ;you will need to note this to be able to use this in a debugger
  push    szHello
  push    0
  call    [MessageBox]

  ;this calls the MessageBox function (specifyed later in the imports section)
  ;then passes it some parameters. The first parameter which tells MessageBox
  ;which window owns our messagebox in this case it's a 0 or NULL which tells
  ;MessageBox that this message box isn't owned by a window and to assign it
  ;to the desktop. the second paramiter is our message, the third is the
  ;messagebox caption (or window name), and the last is the type of messagebox.

  ret
  ;last we need to return control to the OS, there are 2 ways to do this one is
  ;to call ExitProcess the other is to use a 'ret' or return.
  ;(ret only works if you kept your stack balanced properly)
;since we didn't call proc endp isn't needed any more and will just cause an error
;endp
; last is endp, endp is required to tell fasm when the function has ended


;i also like to write my own impoerts section by hand but this is much more advanced and i dont want to confuse you.

section '.idata' import data readable writeable
;this is a special section. the infamous Imports Table used by many windows hackers.
;I'll need to give you a little background for you to understand it completely.
;Every program needs to link to the outside OS. Well the the Imports table is how
;a windows based program does that. When the program first starts the OS scand
;the program for where the imports table is located. after the imports table is found
;the OS looks for all functions it recognizes if it finds one it doesn't know then it
;reports an error and exits the program. if no errors are found then the OS places the
;address of all functions found into the locations specified in the imports table
;structures. This usually all happens transparent to you the programmer (especially in
;C based languages). all you really need to know is that unlike in C and C++ you need to
;tell FASM which API you need.
;(this is not specified in win32a.inc, so win32a.inc != windows.h)

  library kernel, 'KERNEL32.DLL',\
          user,   'USER32.DLL'

;the "library" macro (specified in win32a.inc) tells FASM to create a list of DLL's
;to link to and assign those DLL's a variable name. this macro use a pair sequence first
;the variable name then the library's file name. the ',' separates the parameters (just
;like in c), and the '\' tells FASM to carry the macro/instruction over to the next line.
;in this case were using Kernel32.dll (no case doesn't matter but if you match the case
;of the dll it can help avoid errors) and User32.dll.

  import  kernel,\
          GetModuleHandle,'GetModuleHandleA',\
          ExitProcess,    'ExitProcess'
;the "import" macro (specified in win32a.inc) specify's the format of the imported functions
;in this case we are importing GetModuleHandleA and ExitProcess from kernel (KERNEL32.DLL)
;(Note: some API have two versions standard ASCII and Wide Char. these are specified by the
;'A' and the 'W' on the end of the function.

  import  user,\
          MessageBox,     'MessageBoxA'
Logged


Imagine the impossible, then make it happen.
Shonoby
Programmer
Dr. of C++ology
****
Posts: 659


Pixel Artist


View Profile
« Reply #18 on: May 16, 2007, 01:12:03 AM »

Well, thxz for the tut and the examples, but how do u compile ASM?
Logged

Reality is what we make of it, so by definition we're all living in a fantasy.
- Shonoby
ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1236



View Profile
« Reply #19 on: May 16, 2007, 05:40:54 AM »

well technically you assemble asm. from what i hear anyway. There are many assemblers, but the one that frozenknight and some others here (including i) use is Fasm (so the examples in this thread should work directly with fasm). You can also use asm within c++ with __asm{} i belive it is. though the syntax can and does differ a little.
Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #20 on: May 16, 2007, 12:33:24 PM »

yes the examples here should compile perfectly in FASM, I perfer to use FASM because of the SSSO (Same Source Same Output) adhesion. even with all of myork's power he cannot garente that every source code he writes will compile without problem. FASM solves that problem by forcing users to place all command line switches inside the source file. so WYSIWYG (What You See Is What You Get.) while the macros and includes have a few bugs the assembler is just about perfect except for the fact that the assembler is rather poorly documented. but most of what can be done has been exampled and can be found on their the forums.

I perfer to post here because this community is usually more helpful and more interesting.
Logged


Imagine the impossible, then make it happen.
syazhani
I wonder how long can titles be... because the longer it is the more attention I can get!
Dr. of C++ology
****
Posts: 529


Cats > Dogs


View Profile
« Reply #21 on: September 05, 2008, 01:02:28 PM »

I'm having a look at x86 asm nowadays. So when will CPPLC's sole asm tut be finished?  Tongue
Logged

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."- Brian W. Kernighan

What people misunderstood about Islam presented in comical way: http://ummahfilms.com
FrozenKnight
ASM Freak
Global Moderator
Dr. of C++ology
*****
Posts: 546


Do it yourself it's the only way to learn.


View Profile
« Reply #22 on: January 04, 2009, 10:55:19 PM »

I'm looking into rewriting this because I realized that it is too complicated for the beginner, however this can be used as a reference for understanding parts of ASM. I would like some suggestions for how to begin a new tutorial, i have a habit of over complicating things, so I need to get a better idea of beginners pase.
Logged


Imagine the impossible, then make it happen.
michaelp
baseball 4 life!
rand()%title;
**
Posts: 204



View Profile
« Reply #23 on: January 04, 2009, 11:21:40 PM »

Put in examples of ASM programs using what you've taught. Just explaining commands isn't that useful to a beginner.
Logged


----------------------
oulyt
C++ Freak
***
Posts: 340



View Profile
« Reply #24 on: January 13, 2009, 12:17:48 AM »

^^ very nice. i just got around to reading this.
Logged
Pages: 1 [2]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!