Learning x86 Assembly Language and C/C++ at the same time

Started by MrBogus, February 14, 2008, 12:43:39 PM

Previous topic - Next topic

MrBogus

Hello, I am currently new to this site and I just want to ask if there is any tutorial on how to learn Assemby Language and C/C++ together. I have just already known the basics of C++ but still needs more experience and now, I want to learn assembly language first and while I am learning assembly language I wanted also to translate C++ codes on assembly so I can learn both at the same time. Is there any available tutorial in this site? Or maybe someone could provide some usefull link. Thanks in advance. I will appreciate any useful advice.

ToutEnMasm

Hello,
There is several topics in this forum that show how to mixt c++ and masm.
Quote
http://www.masm32.com/board/index.php?topic=5260.msg39362#msg39362
A good exercice is to translate c++ in masm.Many samples of very good quality are written in c or c++ by microsoft.To translate them you need:
Quote
http://www.masm32.com/board/index.php?topic=8542.msg62123#msg62123

Vortex

Hi MrBogus,

Welcome to the forum.

Why to hurry to learn all the three languages at the same time? In my opinion, you should start with asm or C/C++ for an easier learning curve.

MrBogus

Quote from: ToutEnMasm on February 14, 2008, 01:35:31 PM
Hello,
There is several topics in this forum that show how to mixt c++ and masm.
Quote
http://www.masm32.com/board/index.php?topic=5260.msg39362#msg39362
A good exercice is to translate c++ in masm.Many samples of very good quality are written in c or c++ by microsoft.To translate them you need:
Quote
http://www.masm32.com/board/index.php?topic=8542.msg62123#msg62123

Thanks for you suggestion ToutEnMasm.

Quote from: Vortex on February 14, 2008, 07:04:11 PM
Hi MrBogus,

Welcome to the forum.

Why to hurry to learn all the three languages at the same time? In my opinion, you should start with asm or C/C++ for an easier learning curve.

Hello Vortex,

Thank you for the welcome. I have already learned the basics of C++ but I have decided to learn assembly language first. My plan for now is to translate some C++ codes that I have learned into x86 assembly language code so, I could also understand how a C++ works as well.

By the way, I would like to intoduce my self a little. I'm currently employed as a test  product technician on a semicondutor company somewhere in the Philippines for one and a half year and now, I have fully decided to resign on my job this April so I can focus my self in learning assembly language.

Currently I'm reading The Assembly Programming Master Book by Vlad Pirogov (here is a download link for an ebook for those who are also interested to read/learn http://mihd.net/qc71gn.) and the Win32 ASM tutorial provided by Iczelion http://win32assembly.online.fr/.

jack

here's a book that may be of interest http://www.amazon.com/Visual-C-Optimization-Assembly-Code/dp/193176932X
also if you do a google for visual c++ with assembly you get lot of hits

MrBogus

Jack,

Thanks for the books you have suggested. I will be reading that as soon as I finish the book that I am currently reading.

Currently, I'm memorizing this two Windows Application codes below, (I think this will be the most boring part  :bg ). I decided to memorize this codes first so I can easily disect, understand how each line works and know how the registers work to accomplish the desired tasks.

code from Iczelion's Win32 ASM tutorial 3:

.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib            ; calls to functions in user32.lib and kernel32.lib
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib

WinMain proto :DWORD,:DWORD,:DWORD,:DWORD

.DATA                     ; initialized data
ClassName db "SimpleWinClass",0        ; the name of our window class
AppName db "Our First Window",0        ; the name of our window

.DATA?                ; Uninitialized data
hInstance HINSTANCE ?        ; Instance handle of our program
CommandLine LPSTR ?
.CODE                ; Here begins our code
start:
invoke GetModuleHandle, NULL            ; get the instance handle of our program.
                                                                       ; Under Win32, hmodule==hinstance mov hInstance,eax
mov hInstance,eax
invoke GetCommandLine                        ; get the command line. You don't have to call this function IF
                                                                       ; your program doesn't process the command line.
mov CommandLine,eax
invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT        ; call the main function
invoke ExitProcess, eax                           ; quit our program. The exit code is returned in eax from WinMain.

WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
    LOCAL wc:WNDCLASSEX                                            ; create local variables on stack
    LOCAL msg:MSG
    LOCAL hwnd:HWND

    mov   wc.cbSize,SIZEOF WNDCLASSEX                   ; fill values in members of wc
    mov   wc.style, CS_HREDRAW or CS_VREDRAW
    mov   wc.lpfnWndProc, OFFSET WndProc
    mov   wc.cbClsExtra,NULL
    mov   wc.cbWndExtra,NULL
    push  hInstance
    pop   wc.hInstance
    mov   wc.hbrBackground,COLOR_WINDOW+1
    mov   wc.lpszMenuName,NULL
    mov   wc.lpszClassName,OFFSET ClassName
    invoke LoadIcon,NULL,IDI_APPLICATION
    mov   wc.hIcon,eax
    mov   wc.hIconSm,eax
    invoke LoadCursor,NULL,IDC_ARROW
    mov   wc.hCursor,eax
    invoke RegisterClassEx, addr wc                       ; register our window class
    invoke CreateWindowEx,NULL,\
                ADDR ClassName,\
                ADDR AppName,\
                WS_OVERLAPPEDWINDOW,\
                CW_USEDEFAULT,\
                CW_USEDEFAULT,\
                CW_USEDEFAULT,\
                CW_USEDEFAULT,\
                NULL,\
                NULL,\
                hInst,\
                NULL
    mov   hwnd,eax
    invoke ShowWindow, hwnd,CmdShow               ; display our window on desktop
    invoke UpdateWindow, hwnd                                 ; refresh the client area

    .WHILE TRUE                                                         ; Enter message loop
                invoke GetMessage, ADDR msg,NULL,0,0
                .BREAK .IF (!eax)
                invoke TranslateMessage, ADDR msg
                invoke DispatchMessage, ADDR msg
   .ENDW
    mov     eax,msg.wParam                                            ; return exit code in eax
    ret
WinMain endp

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
    .IF uMsg==WM_DESTROY                           ; if the user closes our window
        invoke PostQuitMessage,NULL             ; quit our application
    .ELSE
        invoke DefWindowProc,hWnd,uMsg,wParam,lParam     ; Default message processing
        ret
    .ENDIF
    xor eax,eax
    ret
WndProc endp

end start



Code from The Assembly Programming Master Book by Vlad Pirogov 


.386P
; Flat memory model
.MODEL FLAT, stdcall
; Constants
; The message arrives when the window is closed
WM_DESTROY          equ 2
; The message arrives when the window is created
WM_CREATE           equ 1
; The message arrives if the left mouse button is clicked

; somewhere in the window area
WM_LBUTTONDOWN      equ 201h
; The message arrives if the right mouse button is clicked
; somewhere in the window area
WM_RBUTTONDOWN      equ 204h
; Window properties
CS_VREDRAW          equ 1h
CS_HREDRAW          equ 2h
CS_GLOBALCLASS      equ 4000h
WS_OVERLAPPEDWINDOW equ 000CF0000H
Style               equ CS_HREDRAW+CS_VREDRAW+CS_GLOBALCLASS
; Standard icon identifier
IDI_APPLICATION     equ 32512
; Cursor identifier
IDC_CROSS           equ 32515
; Normal mode of displaying the window
SW_SHOWNORMAL       equ 1
; Prototypes of external procedures
EXTERN MessageBoxA@16:NEAR
EXTERN CreateWindowExA@48:NEAR
EXTERN DefWindowProcA@16:NEAR
EXTERN DispatchMessageA@4:NEAR
EXTERN ExitProcess@4:NEAR
EXTERN GetMessageA@16:NEAR
EXTERN GetModuleHandleA@4:NEAR
EXTERN LoadCursorA@8:NEAR
EXTERN LoadIconA@8:NEAR
EXTERN PostQuitMessage@4:NEAR
EXTERN RegisterClassA@4:NEAR
EXTERN ShowWindow@8:NEAR
EXTERN TranslateMessage@4:NEAR
EXTERN UpdateWindow@4:NEAR
; Directives for linking libraries
includelib .c:\masm32\lib\user32.lib
includelib c:\masm32\lib\kernel32.lib
;------------------------------------------
; Structures
; Message structure
MSGSTRUCT STRUC
     MSHWND       DD ?   ; Identifier of the window
                         ; that received the message
     MSMESSAGE    DD ?   ; Message identifier
     MSWPARAM     DD ?   ; Auxiliary information about the message

     MSLPARAM     DD ?   ; Auxiliary information about the message
     MSTIME       DD ?   ; Time of sending the message
     MSPT         DD ?   ; Cursor position at the time of sending
                         ; the message

MSGSTRUCT ENDS
;------------
WNDCLASS STRUC
     CLSSTYLE     DD ?   ; Window style
     CLWNDPROC    DD ?   ; Pointer to the window procedure
     CLSCEXTRA    DD ?   ; Information on auxiliary bytes for
                         ; this structure
     CLWNDEXTRA   DD ?   ; Information on auxiliary bytes for the window
     CLSHINSTANCE DD ?   ; Application descriptor
     CLSHICON     DD ?   ; Window icon descriptor
     CLSHCURSOR   DD ?   ; Window cursor descriptor
     CLBKGROUND   DD ?   ; Window brush descriptor
     CLMENUNAME   DD ?   ; Menu identifier
     CLNAME       DD ?   ; Specifies the window class name
WNDCLASS ENDS
; Data segment
_DATA SEGMENT
     NEWHWND      DD 0
     MSG          MSGSTRUCT <?>
     WC           WNDCLASS <?>
     HINST        DD 0 ; Here, the application descriptor is stored
     TITLENAME    DB 'Simple example of a 32-bit application', 0
     CLASSNAME    DB 'CLASS32', 0
     CAP          DB 'Message', 0
     MES1         DB 'You have clicked the left mouse button', 0
     MES2         DB 'Exit. Bye!', 0
_DATA ENDS
; Code segment
_TEXT SEGMENT
START:
; Get application descriptor
    PUSH 0
    CALL GetModuleHandleA@4 ;
    MOV [HINST], EAX
REG_CLASS:
; Fill window structure
; Style
    MOV [WC.CLSSTYLE], style
; Message-handling procedure

MOV   [WC.CLWNDPROC], OFFSET WNDPROC
MOV   [WC.CLSCEXTRA], 0
MOV   [WC.CLWNDEXTRA], 0
MOV   EAX, [HINST]
MOV   [WC.CLSHINSTANCE], EAX
;----------Window icon
PUSH  IDI_APPLICATION
PUSH  0
CALL  LoadIconA@8
MOV   [WC.CLSHICON], EAX
;----------Window cursor
PUSH  IDC_CROSS
PUSH  0
CALL  LoadCursorA@8
MOV   [WC.CLSHCURSOR], EAX
;-----------
MOV   [WC.CLBKGROUND], 17 ; Window color
MOV   DWORD PTR [WC.CLMENUNAME], 0
MOV   DWORD PTR [WC.CLNAME], OFFSET CLASSNAME
PUSH  OFFSET WC
CALL  RegisterClassA@4
; Create a window of the registered class
PUSH  0
PUSH  [HINST]
PUSH  0
PUSH  0
PUSH  400   ; DY - Window height
PUSH  400   ; DX - Window width
PUSH  100   ; Y - Coordinate of the window's top left corner
PUSH  100   ; X - Coordinate of the window's top left corner
PUSH  WS_OVERLAPPEDWINDOW
PUSH  OFFSET TITLENAME ; Window name
PUSH  OFFSET CLASSNAME ; Class name
PUSH  0
CALL  CreateWindowExA@48
; Check for errors
CMP   EAX, 0
JZ    _ERR
MOV   [NEWHWND], EAX ; Window descriptor
;---------------------
PUSH  SW_SHOWNORMAL
PUSH  [NEWHWND]
CALL  ShowWindow@8 ; Display the newly created window

;-------------------------
PUSH  [NEWHWND]
CALL  UpdateWindow@4 ; Redraw the visible part of the window
; WM_PAINT message
; Message-handling loop
MSG_LOOP:
PUSH  0
PUSH  0
PUSH  0
PUSH  OFFSET MSG
CALL  GetMessageA@16
CMP   EAX, 0
JE    END_LOOP
PUSH  OFFSET MSG
CALL  TranslateMessage@4
PUSH  OFFSET MSG
CALL  DispatchMessageA@4
JMP   MSG_LOOP
END_LOOP:
; Exit the program (close the process)
PUSH  [MSG.MSWPARAM]
CALL  ExitProcess@4
_ERR:
JMP   END_LOOP
;------------------------------------------
; Window procedure
; Position of parameters in the stack
; [EBP+014H] LPARAM
; [EBP+10H] WAPARAM
; [EBP+0CH] MES
; [EBP+8] HWND
WNDPROC PROC
    PUSH  EBP
    MOV   EBP, ESP
    PUSH  EBX
    PUSH  ESI
    PUSH  EDI
    CMP   DWORD PTR [EBP+0CH], WM_DESTROY
    JE    WMDESTROY
    CMP   DWORD PTR [EBP+0CH], WM_CREATE
    JE    WMCREATE
    CMP   DWORD PTR [EBP+0CH], WM_LBUTTONDOWN ; Left button
    JE    LBUTTON

    CMP   DWORD PTR [EBP+0CH], WM_RBUTTONDOWN ; Right button
    JE    RBUTTON
    JMP   DEFWNDPROC
; Clicking the right mouse button closes the window
RBUTTON:
    JMP   WMDESTROY
; Clicking the left mouse button
LBUTTON:
; Displaying the message
    PUSH  0 ; MB_OK
    PUSH  OFFSET CAP
    PUSH  OFFSET MES1
    PUSH  DWORD PTR [EBP+08H]
    CALL  MessageBoxA@16
    MOV   EAX, 0
    JMP   FINISH
    WMCREATE:
    MOV   EAX, 0
    JMP   FINISH
    DEFWNDPROC:
    PUSH  DWORD PTR [EBP+14H]
    PUSH  DWORD PTR [EBP+10H]
    PUSH  DWORD PTR [EBP+0CH]
    PUSH  DWORD PTR [EBP+08H]
    CALL  DefWindowProcA@16
    JMP   FINISH
WMDESTROY:
    PUSH  0 ; MB_OK
    PUSH  OFFSET CAP
    PUSH  OFFSET MES2
    PUSH  DWORD PTR [EBP+08H] ; Window descriptor
    CALL  MessageBoxA@16
    PUSH  0
    CALL  PostQuitMessage@4  ; WM_QUIT message
    MOV   EAX, 0
FINISH:
    POP   EDI
    POP   ESI
    POP   EBX
    POP   EBP
    RET   16
WNDPROC ENDP
_TEXT ENDS
END START


From Iczelion's Win32 ASM tutorial, it was simplified by using the export directive instead of this series of command


PUSH par1
PUSH par2
PUSH par3
PUSH par4
CALL NAME_PROC@N ; N - Number of bytes sent to the stack


By the way, the next step that I will be doing is to convert the codes above in C++ code. Unfortunately, I learned only the basics of C++ and still could not do it right now. Is there anyone who can help me do this? Or maybe somebody with a soft heart can translate the code given above. I would appreciate it and that would be a great help for me. My goal for now is just to know the basics of assembly language programming and understanding how registers works upon executing the code. It is going to be complicated for me as I am studying all by my self. I hope I could overcome those complexities with your help. Comments and suggestions are exclusively welcome. Thanks.  :bg