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

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

Writeln in assembler

NASM code for writing the integer in eax to stdout.

segment .bss
        writeTemp       resb    10

segment .text
_writeln:                               
        MOV     ebx,    eax             ; Number is already in eax
        MOV     ecx,    10
        XOR     edi,    edi
L0:
        CMP     eax,    0
        JE      L1
        ADD     edi,    1
        XOR     edx,    edx
        IDIV    ecx
        JMP     L0
L1:
        ADD     edi,    writeTemp       ; Length of the number is in edi
        MOV     esi,    edi
        MOV byte        [edi],  10      ; Add a newline
        SUB     edi,    1
        XOR     eax,    eax
L2:                             
        CMP     ebx,    0
        JE      L3
        MOV     eax,    ebx
        XOR     edx,    edx
        IDIV    ecx
        ADD     dl,     48
        MOV     [edi],  dl
        SUB     edi,    1
        MOV     ebx,    eax
        JMP     L2                      ; Number has been written to writeTemp from tail to head
L3:                             
        MOV     eax,    4
        MOV     ebx,    1
        MOV     ecx,    writeTemp
        MOV     edx,    esi
        SUB     edx,    writeTemp
        ADD     edx,    1
        INT     80h

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