<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: asm code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 06 Oct 2008 12:52:23 GMT</pubDate>
    <description>DZone Snippets: asm code</description>
    <item>
      <title>Writeln in assembler</title>
      <link>http://snippets.dzone.com/posts/show/4516</link>
      <description>NASM code for writing the integer in eax to stdout.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;segment .bss&lt;br /&gt;        writeTemp       resb    10&lt;br /&gt;&lt;br /&gt;segment .text&lt;br /&gt;_writeln:                               &lt;br /&gt;        MOV     ebx,    eax             ; Number is already in eax&lt;br /&gt;        MOV     ecx,    10&lt;br /&gt;        XOR     edi,    edi&lt;br /&gt;L0:&lt;br /&gt;        CMP     eax,    0&lt;br /&gt;        JE      L1&lt;br /&gt;        ADD     edi,    1&lt;br /&gt;        XOR     edx,    edx&lt;br /&gt;        IDIV    ecx&lt;br /&gt;        JMP     L0&lt;br /&gt;L1:&lt;br /&gt;        ADD     edi,    writeTemp       ; Length of the number is in edi&lt;br /&gt;        MOV     esi,    edi&lt;br /&gt;        MOV byte        [edi],  10      ; Add a newline&lt;br /&gt;        SUB     edi,    1&lt;br /&gt;        XOR     eax,    eax&lt;br /&gt;L2:                             &lt;br /&gt;        CMP     ebx,    0&lt;br /&gt;        JE      L3&lt;br /&gt;        MOV     eax,    ebx&lt;br /&gt;        XOR     edx,    edx&lt;br /&gt;        IDIV    ecx&lt;br /&gt;        ADD     dl,     48&lt;br /&gt;        MOV     [edi],  dl&lt;br /&gt;        SUB     edi,    1&lt;br /&gt;        MOV     ebx,    eax&lt;br /&gt;        JMP     L2                      ; Number has been written to writeTemp from tail to head&lt;br /&gt;L3:                             &lt;br /&gt;        MOV     eax,    4&lt;br /&gt;        MOV     ebx,    1&lt;br /&gt;        MOV     ecx,    writeTemp&lt;br /&gt;        MOV     edx,    esi&lt;br /&gt;        SUB     edx,    writeTemp&lt;br /&gt;        ADD     edx,    1&lt;br /&gt;        INT     80h&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 10 Sep 2007 12:14:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4516</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>Automated GNU Makefile</title>
      <link>http://snippets.dzone.com/posts/show/3948</link>
      <description>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:&lt;br /&gt;    PROGNAME: the name of the executable to be built&lt;br /&gt;    CC: the C compiler&lt;br /&gt;    CPP: the C++ compiler&lt;br /&gt;    ASM: the assambler&lt;br /&gt;    LD: the linker&lt;br /&gt;&lt;br /&gt;This searches for all *.c, *.cpp and *.s files and compiles them into objects. It then links all the files into a single executable.&lt;br /&gt;&lt;br /&gt;The strip rule is used to strip all unwanted symbols from the resulting executable. This usually results in a significant (think 40%) size optimisation.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$(VERBOSE).SILENT:&lt;br /&gt;&lt;br /&gt;PROGNAME = prog&lt;br /&gt;&lt;br /&gt;CC = gcc&lt;br /&gt;CC += -c&lt;br /&gt;CPP = g++&lt;br /&gt;CPP += -c&lt;br /&gt;ASM = nasm&lt;br /&gt;ASM += -f elf -d ELF_TYPE&lt;br /&gt;LD = g++&lt;br /&gt;&lt;br /&gt;OBJFILES = $(patsubst %.c,%.o,$(wildcard *.c))&lt;br /&gt;OBJFILES += $(patsubst %.s,%.o,$(wildcard *.s))&lt;br /&gt;OBJFILES += $(patsubst %.cpp,%.o,$(wildcard *.cpp))&lt;br /&gt;&lt;br /&gt;all: $(PROGNAME)&lt;br /&gt;&lt;br /&gt;clean:&lt;br /&gt;	@echo "Cleaning object files"&lt;br /&gt;	@echo "    rm -f     *.o"&lt;br /&gt;	rm -f *.o&lt;br /&gt;	@echo "Cleaning backups"&lt;br /&gt;	@echo "    rm -f     *~"&lt;br /&gt;	rm -f *~&lt;br /&gt;	@echo "Removing programme"&lt;br /&gt;	@echo "    rm -f     "$(PROGNAME)&lt;br /&gt;	rm -f $(PROGNAME)&lt;br /&gt;&lt;br /&gt;%.o: %.s&lt;br /&gt;	@echo "Assambling "$@&lt;br /&gt;	@echo "    ASM       "$&lt;&lt;br /&gt;	$(ASM) $&lt;&lt;br /&gt;&lt;br /&gt;%.o: %.c&lt;br /&gt;	@echo "Compiling "$@&lt;br /&gt;	@echo "    CC        "$&lt;&lt;br /&gt;	$(CC) $&lt;&lt;br /&gt;&lt;br /&gt;%.o: %.cpp&lt;br /&gt;	@echo "Compiling "$@&lt;br /&gt;	@echo "    CPP       "$&lt;&lt;br /&gt;	$(CPP) $&lt;&lt;br /&gt;&lt;br /&gt;$(PROGNAME): $(OBJFILES)&lt;br /&gt;	@echo "Linking "$@&lt;br /&gt;	@echo "    LD        -o "$(PROGNAME)"        "$(OBJFILES)&lt;br /&gt;	$(LD) -o $(PROGNAME) $(OBJFILES)&lt;br /&gt;&lt;br /&gt;strip: $(PROGNAME)&lt;br /&gt;	@echo "Stripping "$(PROGNAME)&lt;br /&gt;	echo -n "Size of "$(PROGNAME)" before strip is "&lt;br /&gt;	ls -sh $(PROGNAME) | cut -d' ' -f1&lt;br /&gt;	@echo "    strip     "$(PROGNAME)&lt;br /&gt;	strip $(PROGNAME)&lt;br /&gt;	echo -n "Size of "$(PROGNAME)" after strip is "&lt;br /&gt;	ls -sh $(PROGNAME) | cut -d' ' -f1&lt;br /&gt;&lt;br /&gt;nothing:&lt;br /&gt;	@echo "Nothing to do; quitting  :("&lt;br /&gt;	@echo "HINT: Try make all"&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 02 May 2007 17:43:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3948</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
  </channel>
</rss>
