Assembly

Questions about programming languages and debugging
Post Reply
User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Assembly

Post by Gogeta70 »

I'm just warning people that assembly is complicated... and the fact that i just spent several hours reading tutorials on how to use it with windows api...

Look at this:

Code: Select all

.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 
All that code, just to make a window...
Honestly, i'd just stick with C++

I'm just not sure i can do this, and i want people to know what they're dealing with if they start assembly...
¯\_(ツ)_/¯ It works on my machine...

User avatar
isapiens
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 533
Joined: 05 May 2006, 16:00
17
Location: Turn around

Post by isapiens »

i am gonna cry. i feel stupid.

User avatar
CommonStray
Forum Assassin
Forum Assassin
Posts: 1215
Joined: 20 Aug 2005, 16:00
18

Post by CommonStray »

Assembly is complicated, but its 1 up from machine code, and even though thats alot of code for just a window, that code will execute faster

User avatar
GODhack
On the way to fame!
On the way to fame!
Posts: 36
Joined: 15 Aug 2005, 16:00
18

Post by GODhack »

ASM is like real modern poetry.
This one is vry nice:

Code: Select all

; thanks to Scali .. he helped optimizing this

.386
.model flat, stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc
includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib

inv     equ invoke

.data
hFile       dd  0
BW          dd  0
ofstruct    OFSTRUCT <>

szPrime     db  "%lu, ", 0
szCaption   db  "Done!", 0
szDone      db  "Done! The number of primes found is: %lu", 13, 10, \
                "Wrote primes to "
szFile      db  "PRIMES.TXT", 0

.data?
buffer      dw  0ffffh  dup (?)
szTemp      db  80 dup (?)

.code
start:

    mov     ecx, 0ffffh / 2         ; counter
    mov     edi, offset buffer      ; here: offset for where to put the numbers
    mov     eax, 00030002h          ; write 2 numbers at one time
    mov     esi, edi                ; where to read the numbers from (for the next step)
createbuffer_loop:
    mov     [edi], eax              ; put 2 numbers into the buffer
    add     eax, 00020002h          ; increase the number that gets put in the buffer
    add     edi, 4                  ; adjust the buffer pointer
    dec     ecx                     ; decrease the counter
    jnz     createbuffer_loop       ; counter <> zero? then do the whole thing again


    mov     edi, esi                ; now restore edi for later
    mov     ecx, 0ffffh / 2         ; counter
    xor     ebx, ebx                ; ebx will hold the number of primes
    xor     edx, edx                ; clear high part of edx, because it will be used as pointer
findprime_loop:
    mov     ax, [esi]               ; get the first word from the buffer
    add     esi, 2                  ; adjust the buffer pointer
    test    ax, ax                  ; ax == zero?
    jz      no_prime                ; then this is already a killed number

    inc     ebx                     ; we are here so this is a prime. increase the prime counter

    mov     dx, ax                  ; put the prime into dx
eliminatenonprime_loop:
    add     dx, ax                  ; now add the prime to the prime (prime*2, later prime*3 ..)
    jc      no_prime                ; if dx now grew over 0ffffh, there's nothing left to kill
    mov     word ptr [edx*2+buffer-4], 0    ; kill non-primes by marking them with zero
    jmp     eliminatenonprime_loop  ; jump back, till dx > 0ffffh

no_prime:
    dec     ecx                     ; decrease the counter
    jnz     findprime_loop          ; counter <> zero? then do it again


    push    ebx                     ; because some APIs change ebx, we have to save it on the stack
    inv     OpenFile, offset szFile, offset ofstruct, OF_CREATE     ; create new file
    mov     hFile, eax              ; save its handle
    mov     cx, 0ffffh              ; put counter to 0ffffh
    xor     eax, eax                ; clear high parts of eax.
write:
    push    cx                      ; save cx on the stack, because one of these APIs will change it
    mov     ax, [edi]               ; normally I'd use esi here, but I restored edi in the beginning
    add     edi, 2                  ; adjust ...
    test    ax, ax                  ; ax == zero?
    jz      skip_write              ; yes? so this is a killed number. do nothing
    inv     wsprintf, offset szTemp, offset szPrime, eax        ; else convert number to ASCII
    inv     WriteFile, hFile, offset szTemp, eax, offset BW, NULL   ; and write it to the file
skip_write:
    pop     cx                      ; restore the counter
    dec     cx                      ; and decrease it
    jnz     write                   ; zero? if not, then go back again

    inv     CloseHandle, hFile      ; close the file

    pop     ebx                     ; pop the prime counter from the stack
    inv     wsprintf, offset szTemp, offset szDone, ebx
    inv     MessageBox, 0, offset szTemp, offset szCaption, MB_OK

    inv     ExitProcess, 0

end start
Autor is 17 years old.

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Post by Gogeta70 »

I guess it's just the API that i hate. It's too damn complicated... *sigh*
¯\_(ツ)_/¯ It works on my machine...

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Post by bad_brain »

well, my OOP knowledge isn´t really helping when I look at these codes... :?
here´s a real nice example which shows the strengths of Assembler:
http://www.cyber-samurai.de/downloads/m ... xmas04.zip

chose "full screen stupidity" and don´t skip until it´s over, else you might miss the good part.. :) the speed is enormous, also the file size is very small, if you would do such a video in Flash it would surely be a couple of megabytes....enjoy! :D

User avatar
FrankB
Ph. D. in Sucko'logics
Ph. D. in Sucko'logics
Posts: 315
Joined: 06 Mar 2006, 17:00
18
Location: Belgistahn
Contact:

Post by FrankB »

bad_brain wrote:well, my OOP knowledge isn´t really helping when I look at these codes... :?
here´s a real nice example which shows the strengths of Assembler:
http://www.cyber-samurai.de/downloads/m ... xmas04.zip
:wow: !
That was a cool one, those guys have seen the entire "The Ring" series, that's for sure :)

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Post by Gogeta70 »

Well... i gave up. I tried, i failed. I guess i'll just stick to php. :cry:
¯\_(ツ)_/¯ It works on my machine...

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Post by bad_brain »

gogeta, it would be stupid if you would NOT stick to php, would be a waste of talent... :wink:

hehe,yeah Frank, you should check out their site, it´s kinda abandoned but there are other files of that kind to find.....besides astonishing graphics stuff there are also REALLY weird apps, one is making your screen producing sounds...but I skipped that one because I feared my monitor will explode... :lol:

Post Reply