Search code examples
assemblyfasm

Declare Functions in ASM x86 FASM assembler


My first attempt at assembly.. I skipped helloworld and decided to just dive right in and do a CRC32Checksum :l

Now I'm confused lol..

My Attempt:

format PE console                                ;Format PE OUT GUI 4.0
entry main

include 'macro/import32.inc'

section '.idata' import data readable           ;Import Section.
library msvcrt,'msvcrt.dll'
import msvcrt, printf, 'printf',\
exit,'exit', getchar, 'getchar'

section '.data' data readable writeable         ;Constants/Static Section.
InitialValue dd 0

section '.code' code readable executable
main:
   jmp CRC32Checksum     ;I want to change this to a call [CRC32Checksum]..
   call [getchar]
   mov eax, 0
   ret 0


CRC32Checksum:
   push ebx
   mov ebp, esp
   mov eax, InitialValue
   NOT eax
   pop ebx
ret

How do I call my CRC32Checksum? Am I even on the right track? How do I declare functions in assembly?


Solution

  • Simply use:

    call CRC32Checksum
    

    In FASM when you bracket something in square brackets it means indirect addressing.

    "call [CRC32Checksum]" means "call the procedure which address is located at CRC32Checksum variable. In practice, you will get compiler error "operand size not specified" because the label CRC32Checksum is code label and has no size assigned. (You can overwrite this by using "call dword [CRC32Checksum]" but here it is meaningless of course).

    Note that you call the imported function "getchar" by indirect call. This is because the imported functions are actually dword variables that contains the address of the imported functions.