Showing posts with label Assembly. Show all posts
Showing posts with label Assembly. Show all posts

Friday, July 19, 2013

How to compile HelloWorld in Intel x86-32 on Mac OSX/FreeBSD

How to compile HelloWorld in Intel x86-32 on Mac OSX/FreeBSD

Compile in your Terminal:
nasm -o hello.tmp -f macho hello.s && ld -arch i386 -macosx_version_min 10.6 -no_pie -e _main -o hello.o hello.tmp && ./hello.o

The Code:
section .data                   ; constants stored here

    msg db "Hello World!", 0xa  ; our string to be printed
    len equ $ - msg             ; get the length of our string

section .text                   ; labels stored here

global _main                    ; specify our main function - (ld -e main)

_syscall:                       ; label - system call - call kernel - how we print to the screen
    int 0x80
    ret

_main:                          ; label - technically int main()
    push    dword len           ; message length
    push    dword msg           ; message to write
    push    dword 1             ; file descriptor - 1 - stdout
    mov     eax, 0x4            ; system call number - 4 - system write
    call    _syscall            ; go to label(function call) - _syscall

    ;  add     esp,12          ;clean stack (3 arguments * 4)

    push    dword 0             ; exit code - return 0
    mov     eax, 0x1            ; system call number (sys_exit)
    call    _syscall            ; go to label(function call) - _syscall

Find more here:
https://github.com/jaredsburrows/Assembly

Sunday, October 21, 2012

How to compile inline Assembly in C

How to compile Assembly inline with C

Make sure you have 32-bit libraries, install them:
apt-get install gcc-multilib

#include <stdio.h>

char Format[] = "Hello world, %d\n"; 

int main (void) { 
 asm ( "subl $8, %esp\n" 
 "movl $3, 4(%esp)\n" 
 "movl $Format, (%esp)\n" 
 "call printf\n" "addl $8, %esp\n" ); 
 return 0; 
}

gcc -m32 test.c -o test.o; ./test.o

http://stackoverflow.com/questions/11378181/use-printf-function-in-inline-asm-on-gcc

Thursday, April 5, 2012

HelloWorld in Assembly

Assembly - HelloWorld
[bits 64]
global _start

section .data
message db "Hello, World!"

section .text
_start:
mov rax, 1
mov rdx, 13
mov rsi, message
mov rdi, 1
syscall

mov rax, 60
mov rdi, 0
syscall
Then run it:
nasm -f elf64 hello.asm
ld hello.o -o hello