Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Alexandru Scvortov

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Hello assambler

The canonical Hello World program in assambler.

Compile it with:
nasm -f elf hello.s
ld -s -o hello hello.o

Helloworld is a function. It uses the write system call to print to stdout (file descriptor 1).

This is written in NASM (Intel) syntax, hence it will not work with gas, tasm or masm. It was written for Linux. The details may vary on other platforms.

Download NASM for free at http://nasm.sourceforge.net/

section .data
  hello: db "Hello world!",10
  hellolen: equ $ - hello

section .text
  global _start

_start:
  call helloworld

  mov eax,1 ; start exit procedure
  mov ebx,0

  int 80h

helloworld:
  mov eax,4 ; 4 is the code for the write system call
  mov ebx,1 ; 1 is the fd for stdout
  mov ecx,hello ; the address of the string
  mov edx,hellolen ; the length of the string

  int 80h ; trigger an interrupt; this should be 21h for DOS

  ret

Automated GNU Makefile

A simple automated GNU Makefile to build and link c/c++/asm projects. All the variables that need to be tweaked are located at the begining:
PROGNAME: the name of the executable to be built
CC: the C compiler
CPP: the C++ compiler
ASM: the assambler
LD: the linker

This searches for all *.c, *.cpp and *.s files and compiles them into objects. It then links all the files into a single executable.

The strip rule is used to strip all unwanted symbols from the resulting executable. This usually results in a significant (think 40%) size optimisation.

$(VERBOSE).SILENT:

PROGNAME = prog

CC = gcc
CC += -c
CPP = g++
CPP += -c
ASM = nasm
ASM += -f elf -d ELF_TYPE
LD = g++

OBJFILES = $(patsubst %.c,%.o,$(wildcard *.c))
OBJFILES += $(patsubst %.s,%.o,$(wildcard *.s))
OBJFILES += $(patsubst %.cpp,%.o,$(wildcard *.cpp))

all: $(PROGNAME)

clean:
	@echo "Cleaning object files"
	@echo "    rm -f     *.o"
	rm -f *.o
	@echo "Cleaning backups"
	@echo "    rm -f     *~"
	rm -f *~
	@echo "Removing programme"
	@echo "    rm -f     "$(PROGNAME)
	rm -f $(PROGNAME)

%.o: %.s
	@echo "Assambling "$@
	@echo "    ASM       "$<
	$(ASM) $<

%.o: %.c
	@echo "Compiling "$@
	@echo "    CC        "$<
	$(CC) $<

%.o: %.cpp
	@echo "Compiling "$@
	@echo "    CPP       "$<
	$(CPP) $<

$(PROGNAME): $(OBJFILES)
	@echo "Linking "$@
	@echo "    LD        -o "$(PROGNAME)"        "$(OBJFILES)
	$(LD) -o $(PROGNAME) $(OBJFILES)

strip: $(PROGNAME)
	@echo "Stripping "$(PROGNAME)
	echo -n "Size of "$(PROGNAME)" before strip is "
	ls -sh $(PROGNAME) | cut -d' ' -f1
	@echo "    strip     "$(PROGNAME)
	strip $(PROGNAME)
	echo -n "Size of "$(PROGNAME)" after strip is "
	ls -sh $(PROGNAME) | cut -d' ' -f1

nothing:
	@echo "Nothing to do; quitting  :("
	@echo "HINT: Try make all"



« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS