Author Topic: SDL 2 Hello World NASM GCC  (Read 8818 times)

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
SDL 2 Hello World NASM GCC
« on: October 02, 2013, 01:40:13 PM »
Hello!

; *************************************************
; *************************************************
; *************************************************
; This is: SDL 2 Hello World NASM GCC.
; Author: J.K. Encryptor256.
; Date: October 02, year of 2013.
; *************************************************
; Using: Netwide assembler and GCC.
; Nasm: Produces .O file.
; GCC: Produces EXE file, from .O file. (Just 28,008 bytes of EXE)
; *************************************************
; Compile:
; Nasm: "nasm -f win32 program.asm".
; Gcc: "gcc -m32 -o program.exe program.o  -lsdl2 -LK:/win32/sdl/helloworld".
; ---
; GCC arg -L is the Path where sdl2.dll is located: "K:/win32/sdl/helloworld"
; *************************************************
; Intro:
; I found SDL 2 tutorial on the web: twinklebeardev.blogspot.com.
; So i learned it, created in my own way,
; translated into NASM assembly.
; Also, SDL 2 function description i found on web: wiki.libsdl.org.
; *************************************************
; +Program Requires:
; 1. Bitmap: helloGCC.bmp
; 2. Library: sdl2.dll
; *************************************************
; +Description:
; Program loads helloGCC.bmp,
; displays it on the application owned screen.
; Program terminates upon keypress or ordinary quit.
; *************************************************
; *************************************************
; *************************************************

.LET_CODE_BEGIN:

Code: [Select]
; ############################################
; # Create macro, the way function names are defined

%macro cextern 1                       ; cextern printf
        extern _%1                         ;         extern _printf
        %ifndef %1                         ;        %ifndef printf
                %define %1 _%1        ;                %define printf _printf
        %endif                                ;        %endif
%endmacro                                 ; %endmacro

%macro cglobal 1                        ; cglobal main
        global _%1                          ;         global _main
        %ifndef %1                          ;        %ifndef main
                %define %1 _%1        ;                %define main _main
        %endif                                 ;        %endif
%endmacro                                 ; %endmacro

; # Tell compiler what cpu type to use
cpu 386

; # Tell compiler to generate 32 bit code
bits 32

; # Define some constants

        ; SDL constants ------------------------------------------------

                SDL_INIT_TIMER equ 0x00000001 ; timer subsystem
                SDL_INIT_AUDIO equ 0x00000010 ; audio subsystem
                SDL_INIT_VIDEO equ 0x00000020 ; video subsystem
                SDL_INIT_JOYSTICK equ 0x00000200 ; joystick subsystem
                SDL_INIT_HAPTIC equ 0x00001000 ; haptic (force feedback) subsystem
                SDL_INIT_GAMECONTROLLER equ 0x00002000 ; controller subsystem
                SDL_INIT_EVENTS  equ 0x00004000 ; events subsystem
                SDL_INIT_NOPARACHUTE equ 0x00100000 ; don't catch fatal signals

                SDL_INIT_EVERYTHING equ SDL_INIT_TIMER \
                                                        + SDL_INIT_AUDIO \
                                                        + SDL_INIT_VIDEO \
                                                        + SDL_INIT_JOYSTICK \
                                                        + SDL_INIT_HAPTIC \
                                                        + SDL_INIT_GAMECONTROLLER \
                                                        + SDL_INIT_EVENTS

        ; SDL Window  ------------------------------------------------

                SDL_WINDOW_SHOWN equ 0x00000004
                SDL_WINDOWPOS_CENTERED_MASK equ 0x2FFF0000

        ; SDL Renerer ------------------------------------------------

                SDL_RENDERER_ACCELERATED equ 0x00000002
                SDL_RENDERER_PRESENTVSYNC equ 0x00000004

        ; SDL Event type ------------------------------------------------

                SDL_QUIT equ 0x100
                SDL_KEYDOWN equ 0x300

; # Use our predefined macro

        cextern SDL_Init
        cextern SDL_ClearError
        cextern SDL_GetError
        cextern SDL_CreateWindow
        cextern SDL_CreateRenderer
        cextern SDL_RWFromFile
        cextern SDL_LoadBMP_RW
        cextern SDL_CreateTextureFromSurface
        cextern SDL_FreeSurface
        cextern SDL_RenderClear
        cextern SDL_RenderCopy
        cextern SDL_RenderPresent
        cextern SDL_Delay

        cextern SDL_PollEvent
        cextern SDL_Quit

        cglobal main
 

;# Data segment
; ---------------------------------------------------------------
segment .data use32
       
        txt_bmp_path db "helloGCC.bmp",0
        txt_file_access_rb db "rb",0,0
        txt_error db "! Error: ",0
        txt_window db "Hello World: J.K. Encryptor 256, October 02, year of 2013.",0

       
;# Uninitialized data segment
; ---------------------------------------------------------------
segment .bss use32
       
        hWindow resd 1
        hRenderer resd 1
        hFile resd 1
        hBitmap resd 1
        hTexture resd 1

        hEventBuffer:
                        .type resd 1
                        .other resb 256

;# Code segment
; ---------------------------------------------------------------
segment .text use32
       
;Info: This "start" label will tell, the compiler, where execution begins.

main:
        push ebp
        mov ebp,esp
       
        ; # SDL_Init:
        ; # Use this function to initialize the SDL library.
        ; # This must be called before using any other SDL function.
        ; # --------------------------------------------------------------------------

                push dword SDL_INIT_EVERYTHING

                call SDL_Init

                add esp,dword 4*1 ; pop pushed items off the stack

        ; # SDL_ClearError:
        ; # Use this function to clear any previous error message.
        ; # --------------------------------------------------------------------------

                call SDL_ClearError

        ; # SDL_CreateWindow:
        ; # Use this function to create a window
        ; # with the specified position, dimensions, and flags.
        ; # --------------------------------------------------------------------------

                push dword SDL_WINDOW_SHOWN
                push dword 480
                push dword 640
                push dword SDL_WINDOWPOS_CENTERED_MASK
                push dword SDL_WINDOWPOS_CENTERED_MASK
                push dword txt_window

                call SDL_CreateWindow

                add esp,dword 4*6 ; pop pushed items off the stack

                mov dword [hWindow],eax

        ; # SDL_CreateRenderer:
        ; # Use this function to create a 2D rendering context for a window.
        ; # --------------------------------------------------------------------------

                push dword (SDL_RENDERER_ACCELERATED+SDL_RENDERER_PRESENTVSYNC)
                push dword -1
                push dword [hWindow]

                call SDL_CreateRenderer

                add esp,dword 4*3 ; pop pushed items off the stack

                mov dword [hRenderer],eax

        ; # SDL_LoadBMP:
        ; # Use this function to load a surface from a BMP file.
        ; # --------------------------------------------------------------------------

                ; # SDL_RWFromFile:
                ; # Use this function to create a new SDL_RWops structure for
                ; # reading from and/or writing to a named file.
                ; -----------------------------------------------------------------

                        push dword txt_file_access_rb
                        push dword txt_bmp_path

                        call SDL_RWFromFile

                        add esp,dword 4*2 ; pop pushed items off the stack

                        mov dword [hFile],eax

                ; # SDL_LoadBMP_RW:
                ; # Use this function to load a BMP image
                ; # from a seekable SDL data stream (memory or file).
                ; -----------------------------------------------------------------

                        push dword 1
                        push dword [hFile]

                        call SDL_LoadBMP_RW

                        add esp,dword 4*2 ; pop pushed items off the stack

                        mov dword [hBitmap],eax

        ; # SDL_CreateTextureFromSurface:
        ; # Use this function to create a texture from an existing surface.
        ; # --------------------------------------------------------------------------

                push dword [hBitmap]
                push dword [hRenderer]

                call SDL_CreateTextureFromSurface

                add esp,dword 4*2 ; pop pushed items off the stack

                mov dword [hTexture],eax

        ; # SDL_FreeSurface:
        ; # Use this function to free an RGB surface.
        ; # --------------------------------------------------------------------------

                push dword [hBitmap]

                call SDL_FreeSurface

                add esp,dword 4*1 ; pop pushed items off the stack

        ; # SDL_RenderClear:
        ; # Use this function to clear the current rendering target with the drawing color.
        ; # --------------------------------------------------------------------------

                push dword [hRenderer]

                call SDL_RenderClear

                add esp,dword 4*1 ; pop pushed items off the stack

        ; # SDL_RenderCopy:
        ; # Use this function to copy a portion
        ; # of the texture to the current rendering target.
        ; # --------------------------------------------------------------------------

                push dword 0
                push dword 0
                push dword [hTexture]
                push dword [hRenderer]

                call SDL_RenderCopy

                add esp,dword 4*4 ; pop pushed items off the stack

        ; # SDL_RenderPresent:
        ; # Use this function to update the screen with rendering performed.
        ; # --------------------------------------------------------------------------

                push dword [hRenderer]

                call SDL_RenderPresent

                add esp,dword 4*1 ; pop pushed items off the stack


        ; # Main loop:
        ; # --------------------------------------------------------------------------

        .mainLoop:

                ; # SDL_PollEvent:
                ; # Use this function to poll for currently pending events.
                ; # --------------------------------------------------------------------------
                       
                        push dword hEventBuffer

                        call SDL_PollEvent

                        add esp,dword 4*1 ; pop pushed items off the stack

                        cmp eax,dword 0

                        jle .noEvents

                ; # SDL_QUIT:
                ; # --------------------------------------------------------------------------

                        cmp dword [hEventBuffer.type], dword SDL_QUIT

                        je .quitMainLoop

                ; # SDL_KEYDOWN:
                ; # --------------------------------------------------------------------------

                        cmp dword [hEventBuffer.type], dword SDL_KEYDOWN

                        je .quitMainLoop

                ; # --------------------------------------------------------------------------

                .noEvents:
               

                jmp .mainLoop

        .quitMainLoop:

        ; # SDL_Quit:
        ; # Use this function to clean up all initialized subsystems.
        ; # You should call it upon all exit conditions.
        ; # --------------------------------------------------------------------------

                call SDL_Quit

        ; # ExitProcess:
        ; # --------------------------------------------------------------------------

                mov eax,dword 0
                pop ebp
                ret

; ----
; END
; ----



.VIDEO:

Tired of reading?

Watch video on youtube named: "SDL 2 Hello World NASM GCC"

Link: http://youtu.be/3SS-3UPMdJg

.CODE:

Added attachment of code file, bmp file, dll file.

.END:

Bye!

Have a nice day and let bytes protect you! :D

Encryptor256



Encryptor256's Investigation \ Research Department.