<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: assembler code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 08:09:20 GMT</pubDate>
    <description>DZone Snippets: assembler code</description>
    <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>Converting diskette LBA to CHS</title>
      <link>http://snippets.dzone.com/posts/show/3130</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;Put LBA address in AX and get your results in CL, CH and DH.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;;-----------------------------------------------------------------------------;&lt;br /&gt;;		DISKETTE_LBA_TO_CHS(AX LBA) CL Head, CH Cylinder, DH Sector   ;&lt;br /&gt;;-----------------------------------------------------------------------------;&lt;br /&gt;diskette_lba_to_chs:&lt;br /&gt;	xor dx, dx&lt;br /&gt;	mov bx, 18&lt;br /&gt;        div bx&lt;br /&gt;	push dx				; = Stack(1)			&lt;br /&gt;	mov dx, ax&lt;br /&gt;        shr ax, 2&lt;br /&gt;	mov cl, al&lt;br /&gt;	mov ch, dl&lt;br /&gt;	pop dx				; = Stack(0)&lt;br /&gt;	inc dl&lt;br /&gt;	mov dh, dl&lt;br /&gt;	ret&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 12 Dec 2006 02:05:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3130</guid>
      <author>darktemplar (Piotr)</author>
    </item>
    <item>
      <title>[ASM]Write String only using BIOS</title>
      <link>http://snippets.dzone.com/posts/show/3129</link>
      <description>First, set starting address of text into DS:SI registers. Procedure writes text until null-character is met.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;;-----------------------------------------------------------------------------;&lt;br /&gt;;		WRITE_STRING(DS:SI Text)                                      ;&lt;br /&gt;;-----------------------------------------------------------------------------;&lt;br /&gt;;	Writes string from DS:SI until character #0 is met		      ;&lt;br /&gt;Write_String:		&lt;br /&gt;	mov ah, 0xE	&lt;br /&gt;	xor bh, bh	&lt;br /&gt;	mov bl, 0x7&lt;br /&gt;.nextchar	&lt;br /&gt;	lodsb		&lt;br /&gt;	or al,al&lt;br /&gt;	jz .return&lt;br /&gt;	int 10h	&lt;br /&gt;	jmp .nextchar&lt;br /&gt;.return		&lt;br /&gt;	ret	&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 12 Dec 2006 02:02:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3129</guid>
      <author>darktemplar (Piotr)</author>
    </item>
  </channel>
</rss>
