<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: nasm code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 11 Oct 2008 21:02:20 GMT</pubDate>
    <description>DZone Snippets: nasm 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>Easter date - NASM</title>
      <link>http://snippets.dzone.com/posts/show/3982</link>
      <description>Compute Easter date in NASM.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;segment .text&lt;br /&gt;  global easter_asm&lt;br /&gt;&lt;br /&gt;;typedef struct {&lt;br /&gt;;  unsigned short d;&lt;br /&gt;;  unsigned short m;&lt;br /&gt;;} Date;&lt;br /&gt;&lt;br /&gt;;void easter_asm(unsigned short Y, Date *d)&lt;br /&gt;;   computes the Easter date for the year Y and stores it in d&lt;br /&gt;;&lt;br /&gt;;Parameters&lt;br /&gt;;   Y - the year&lt;br /&gt;;   d - the Date struct in which to store the result&lt;br /&gt;&lt;br /&gt;%define d [ebp + 12]&lt;br /&gt;%define Y [ebp + 8]&lt;br /&gt;easter_asm:&lt;br /&gt;  enter 0, 0&lt;br /&gt;  sub esp, 28&lt;br /&gt;  %define G [ebp - 4]&lt;br /&gt;  %define C [ebp - 8]&lt;br /&gt;  %define X [ebp - 12]&lt;br /&gt;  %define Z [ebp - 16]&lt;br /&gt;  %define D [ebp - 20]&lt;br /&gt;  %define E [ebp - 24]&lt;br /&gt;  %define N [ebp - 28]&lt;br /&gt;&lt;br /&gt;  ; int G = (Y % 19) + 1;&lt;br /&gt;  xor edx, edx&lt;br /&gt;  mov eax, Y&lt;br /&gt;  mov ebx, 19&lt;br /&gt;  div ebx&lt;br /&gt;  inc edx&lt;br /&gt;  mov G, edx&lt;br /&gt;&lt;br /&gt;  ; int C = (int)(Y / 100) + 1;&lt;br /&gt;  xor edx, edx&lt;br /&gt;  mov eax, Y&lt;br /&gt;  mov ebx, 100&lt;br /&gt;  div ebx&lt;br /&gt;  inc eax&lt;br /&gt;  mov C, eax&lt;br /&gt;&lt;br /&gt;  ; int X = 3 * C / 4 - 12;&lt;br /&gt;  xor edx, edx&lt;br /&gt;  mov eax, C&lt;br /&gt;  mov ebx, 4&lt;br /&gt;  div ebx&lt;br /&gt;  mov ebx, 3&lt;br /&gt;  mul ebx&lt;br /&gt;  sub eax, 12&lt;br /&gt;  mov X, eax&lt;br /&gt;&lt;br /&gt;  ; int Z = (8 * C + 5) / 25 - 5;&lt;br /&gt;  xor edx, edx&lt;br /&gt;  mov eax, C&lt;br /&gt;  mov ebx, 8&lt;br /&gt;  mul ebx&lt;br /&gt;  add eax, 5&lt;br /&gt;  mov ebx, 25&lt;br /&gt;  div ebx&lt;br /&gt;  sub eax, 5&lt;br /&gt;  mov Z, eax&lt;br /&gt;&lt;br /&gt;  ; int D = 5 * Y / 4 - X - 10;&lt;br /&gt;  xor edx, edx&lt;br /&gt;  mov eax, Y&lt;br /&gt;  mov ebx, 5&lt;br /&gt;  mul ebx,&lt;br /&gt;  mov ebx, 4&lt;br /&gt;  div ebx&lt;br /&gt;  sub eax, X&lt;br /&gt;  sub eax, 10&lt;br /&gt;  mov D, eax&lt;br /&gt;&lt;br /&gt;  ; int E = (11 * G + 20 + Z - X) % 30;&lt;br /&gt;  mov eax, G&lt;br /&gt;  mov ebx, 11&lt;br /&gt;  mul ebx&lt;br /&gt;  add eax, 20&lt;br /&gt;  add eax, Z&lt;br /&gt;  sub eax, X&lt;br /&gt;  xor edx, edx&lt;br /&gt;  mov ebx, 30&lt;br /&gt;  div ebx&lt;br /&gt;  mov E, edx&lt;br /&gt;&lt;br /&gt;  cmp dword E, 24&lt;br /&gt;  jne after_e1&lt;br /&gt;  inc dword E&lt;br /&gt;  jmp after_e2&lt;br /&gt;after_e1:&lt;br /&gt;  cmp dword E, 25&lt;br /&gt;  jne after_e2&lt;br /&gt;  cmp dword G, 11&lt;br /&gt;  jle after_e2&lt;br /&gt;  inc dword E&lt;br /&gt;after_e2:&lt;br /&gt;&lt;br /&gt;  ; int N = 44 - E;&lt;br /&gt;  mov eax, 44&lt;br /&gt;  sub eax, E&lt;br /&gt;  mov N, eax&lt;br /&gt;&lt;br /&gt;  cmp dword N, 21&lt;br /&gt;  jge after_n&lt;br /&gt;  add dword N, 30&lt;br /&gt;after_n:&lt;br /&gt;  ; N = N + 7 - ((D + N) % 7);&lt;br /&gt;  mov eax, N&lt;br /&gt;  add eax, D&lt;br /&gt;  xor edx, edx&lt;br /&gt;  mov ebx, 7&lt;br /&gt;  div ebx&lt;br /&gt;  add dword N, 7&lt;br /&gt;  sub N, edx&lt;br /&gt;&lt;br /&gt;  mov eax, d&lt;br /&gt;&lt;br /&gt;  ; N is the nuber of day from March 1 to easter. Check if easter is in April.&lt;br /&gt;  cmp dword N, 31&lt;br /&gt;  jle in_march&lt;br /&gt;  mov bl, N&lt;br /&gt;  sub bl, 31&lt;br /&gt;  mov [eax], bl&lt;br /&gt;  mov word [eax + 2], 4&lt;br /&gt;  jmp quit&lt;br /&gt;in_march:&lt;br /&gt;  mov bl, N&lt;br /&gt;  mov [eax], bl&lt;br /&gt;  mov word [eax + 2], 3&lt;br /&gt;quit:&lt;br /&gt;  leave&lt;br /&gt;  ret&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 10 May 2007 08:17:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3982</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>Assembler - Insert sort</title>
      <link>http://snippets.dzone.com/posts/show/3968</link>
      <description>This is a straightforward implementation of the insert sort algorithm in assembler (NASM).&lt;br /&gt;&lt;br /&gt;Compile and link it with:&lt;br /&gt;nasm -f elf isort.s&lt;br /&gt;ld -s -o isort isort.o&lt;br /&gt;&lt;br /&gt;Insert sort: http://en.wikipedia.org/wiki/Insert_sort&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;; void isort(int *a, int n)&lt;br /&gt;;   sorts the first n elements of a&lt;br /&gt;;&lt;br /&gt;; Parameters&lt;br /&gt;;   a - pointer to the array&lt;br /&gt;;   n - number of elements to sorts&lt;br /&gt;&lt;br /&gt;%define a [ebp + 8]&lt;br /&gt;%define n [ebp + 12]&lt;br /&gt;isort:&lt;br /&gt;  enter 0, 0&lt;br /&gt;  pusha&lt;br /&gt;&lt;br /&gt;  mov ecx, 1&lt;br /&gt;  for:&lt;br /&gt;    mov ebx, ecx&lt;br /&gt;    imul ebx, 4&lt;br /&gt;    add ebx, a&lt;br /&gt;    mov ebx, [ebx]&lt;br /&gt;&lt;br /&gt;    mov edx, ecx&lt;br /&gt;    dec edx&lt;br /&gt;&lt;br /&gt;    while:&lt;br /&gt;      cmp edx, 0&lt;br /&gt;      jl while_quit&lt;br /&gt;&lt;br /&gt;      mov eax, edx&lt;br /&gt;      imul eax, 4&lt;br /&gt;      add eax, a&lt;br /&gt;&lt;br /&gt;      cmp ebx, [eax]&lt;br /&gt;      jge while_quit&lt;br /&gt;&lt;br /&gt;      mov esi, [eax]&lt;br /&gt;&lt;br /&gt;      mov dword [eax + 4], esi&lt;br /&gt;&lt;br /&gt;      dec edx&lt;br /&gt;      jmp while&lt;br /&gt;    while_quit:&lt;br /&gt;&lt;br /&gt;    mov [eax], ebx&lt;br /&gt;&lt;br /&gt;    inc ecx&lt;br /&gt;    cmp ecx, n&lt;br /&gt;    jl for&lt;br /&gt;&lt;br /&gt;  popa&lt;br /&gt;  leave&lt;br /&gt;  ret&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 08 May 2007 19:32:03 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3968</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>Hello assambler</title>
      <link>http://snippets.dzone.com/posts/show/3949</link>
      <description>The canonical Hello World program in assambler.&lt;br /&gt;&lt;br /&gt;Compile it with:&lt;br /&gt;nasm -f elf hello.s&lt;br /&gt;ld -s -o hello hello.o&lt;br /&gt;&lt;br /&gt;Helloworld is a function. It uses the write system call to print to stdout (file descriptor 1).&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;Download NASM for free at http://nasm.sourceforge.net/&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;section .data&lt;br /&gt;  hello: db "Hello world!",10&lt;br /&gt;  hellolen: equ $ - hello&lt;br /&gt;&lt;br /&gt;section .text&lt;br /&gt;  global _start&lt;br /&gt;&lt;br /&gt;_start:&lt;br /&gt;  call helloworld&lt;br /&gt;&lt;br /&gt;  mov eax,1 ; start exit procedure&lt;br /&gt;  mov ebx,0&lt;br /&gt;&lt;br /&gt;  int 80h&lt;br /&gt;&lt;br /&gt;helloworld:&lt;br /&gt;  mov eax,4 ; 4 is the code for the write system call&lt;br /&gt;  mov ebx,1 ; 1 is the fd for stdout&lt;br /&gt;  mov ecx,hello ; the address of the string&lt;br /&gt;  mov edx,hellolen ; the length of the string&lt;br /&gt;&lt;br /&gt;  int 80h ; trigger an interrupt; this should be 21h for DOS&lt;br /&gt;&lt;br /&gt;  ret&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 02 May 2007 18:40:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3949</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
  </channel>
</rss>
