www.main.lv
Don't think just code it

Search results for 'freebsd'

2011-02-18 Intel/Linux/BSD system

FreeBSD assembler sample:
Tools
Simple programm
Hello world
Hello world + libc
C + asm
Links where is somthing useful
Files
Open File

Linux assembler samples:
Hello World
gcc + asm
g++ + asm
Open file 
Make directory

SDL assembler example
SDL programming

FPU Topics
Calculating polinom

SSE
SSE add

Programming sample from various themes.
Basic HTTP server
FPU catch division by zero
BIn2Hex converter
ReprBin
Arp Packet Analyzer
Keyboard LED flush
PC speaker
Xlib, hello world

Interesting themes:
Linux Format String Attack
ELF rewrite function
Assembler scripting language
ELF text section
Linux ShellCode 1
Local Descriptor Table
Nano bug (CVS 2010-1160)
Hooking interrupt descriptor table

Antidebug
Antidebug 1
Antidebug 2
Antidebug 3

2009-09-04 FreeBSD assembler open file

Here code for opening file, reading from it and close it. At beginig i have thinked taht it will be complicated. But it was easy and interesting as C. Here is both C and asm code.
openfile.asm

;/usr/include/sys/syscall.h
;#define SYS_read        3
;#define SYS_write       4
;#define SYS_open        5
;#define SYS_close       6

sys_read equ 3
sys_write equ 4
sys_open equ 5
sys_close equ 6
o_rdonly equ 0

format ELF
section '.text' executable
public _start
_start:
    ;int fid = open("file.txt",O_RDONLY);
    push o_rdonly
    push f
    mov  eax, sys_open 
    push eax
    int  0x80
    add  esp, 4*3   ; clear stack after interupt	
    mov dword [f_id], eax
	
    ;read( fid , &buf[0] , 12 );
    push f_buf_len
    push f_buf
    push eax
    mov eax, sys_read
    push eax
    int 0x80
    add esp, 4*4
	
    ;write( 1 , &buf[0] , 12 );
    push f_buf_len   
    push f_buf     
    push 1         
    mov  eax, sys_write     
    push eax
    int  0x80
    add  esp,4*3

    ;close( fid );
    push dword [f_id]
    mov eax, sys_close
    push eax
    int 0x80
    add esp, 4*2
	
    ;exit from programm
    xor eax, eax	;eax = 0
    push eax
    inc eax			;eax = 1, sys_exit
    int 80h			;system interupt

section '.data' writeable
	f db "file.txt",0
	f_len = $-f
	f_buf db 12 dup 0
	f_buf_len = $-f_buf
	f_id dd 0

fasm openfile.asm openfile.o
ld openfile.o -o openfile

c.c
#include <fcntl.h>

int main()
{
	int fid = open("file.txt",O_RDONLY);
	char buf[12];
	read( fid , &buf[0] , 12 );
	write( 1 , &buf[0] , 12 );
	close( fid );
	return 0;
}


gcc c.c -o c

file.txt
Only text!!!

2009-04-07 FreeBSD assembler

I like assembler as it is very simple to learn but you should know how works your HardWare.
At this moment on my computer I have FreeBSD7.1. That whay i will write some examples about assembler for FreeBSD. With some little modifications that code also can be launched on Linux machines. Ou I forget assembler will oriented on Intel architecture. Don't forget AMD also understand Intel commands.

Tools
Simple programm
Hello world
Hello world + libc
C + asm
Links where is somthing useful
Files
Open File

2009-04-07 FreeBSD assembler asm from C

This is example how to combine assemblers code with C code
Filename: main.c

extern out_f();
int main()
{
	out_f();
	return 0;
}

Here is assemblers code
Filename: out.asm
format ELF
section '.text' executable
public out_f
out_f:
	push msg_len
	push msg
	push 1
	mov eax, 4
	push eax
	int 0x80
	add esp, 4*4
	ret
section '.data' writeable
msg db "Hello from out.asm",0
msg_len = $-msg

Compiling lines is:
gcc -c main.c -o main.o
fasm out.asm out.o
gcc main.o out.o -o main

2009-03-28 FreeBSD assembler links

Webster
Optimisations manuals
Linux Assembly
Links on russian
FreeBSD with FASM
FASM+OpenGL

2009-03-28 FreeBSD assembler hello world

It is our hello world in asm.

format ELF
section '.text' executable
public _start
_start:	
	push msg_len   ; size of message
	push msg       ; offset of message
	push 1         ; stdout
	mov  eax,4     ; 4 =  sys_write
	push eax
	int  0x80
	add  esp,4*3   ; clear stack after interupt
	;exit from programm
	xor eax, eax	;eax = 0
	push eax		
	inc eax			;eax = 1, sys_exit
	int 80h			;system interupt
section '.data' writeable
	msg db "Hello world",0
	msg_len = $-msg

Compilations is easy as before
fasm hello.asm hello.o
ld -o hello hello.o

2009-03-28 FreeBSD assembler hello world libc

Now our hello world becomes more portable becouse it uses standart libc library. You can use power of standart libraries. Also programms code become smaller

format ELF
section '.text' executable
extrn printf
public main
main:
        push msg
        call printf
        add  esp,4
        ret
section '.data' writeable
        msg db "Hello world!",0

There is small diference when compiling this file
fasm hellolibc.asm hellolibc.o
gcc -o hellolibc hellolibc.o

2009-03-28 FreeBSD assembler simple programm

Programm should start with _start point . This programm that only exit. That is minimal what we need start programm and terminate it.

format ELF 
section '.text' 
executable public _start 
_start: 
    xor eax,eax 
    push eax
    inc eax; 1 = sys_exit 
    int 0x80

Compiling this with fasm simple.asm -o simple.o
Then running ld to link our first programm
ld -o simple simple.o

2009-03-28 FreeBSD assembler what you need

FASM
It is flat assembler it is simple and fast. I like to use it because it don't have to many options when compiling.
Download Place for FASM

LD
It is GNU linker how it can be used described in man pages
GCC C
GCC C compiler will help when we using libc or using both asm and C.
HEXDUMP
hexdump will help see what is inside files.
Disassembler
You can select anyone. Iam using HTE.
Download Place for HTE