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 

Easter date - NASM

Compute Easter date in NASM.

   1  
   2  segment .text
   3    global easter_asm
   4  
   5  ;typedef struct {
   6  ;  unsigned short d;
   7  ;  unsigned short m;
   8  ;} Date;
   9  
  10  ;void easter_asm(unsigned short Y, Date *d)
  11  ;   computes the Easter date for the year Y and stores it in d
  12  ;
  13  ;Parameters
  14  ;   Y - the year
  15  ;   d - the Date struct in which to store the result
  16  
  17  %define d [ebp + 12]
  18  %define Y [ebp + 8]
  19  easter_asm:
  20    enter 0, 0
  21    sub esp, 28
  22    %define G [ebp - 4]
  23    %define C [ebp - 8]
  24    %define X [ebp - 12]
  25    %define Z [ebp - 16]
  26    %define D [ebp - 20]
  27    %define E [ebp - 24]
  28    %define N [ebp - 28]
  29  
  30    ; int G = (Y % 19) + 1;
  31    xor edx, edx
  32    mov eax, Y
  33    mov ebx, 19
  34    div ebx
  35    inc edx
  36    mov G, edx
  37  
  38    ; int C = (int)(Y / 100) + 1;
  39    xor edx, edx
  40    mov eax, Y
  41    mov ebx, 100
  42    div ebx
  43    inc eax
  44    mov C, eax
  45  
  46    ; int X = 3 * C / 4 - 12;
  47    xor edx, edx
  48    mov eax, C
  49    mov ebx, 4
  50    div ebx
  51    mov ebx, 3
  52    mul ebx
  53    sub eax, 12
  54    mov X, eax
  55  
  56    ; int Z = (8 * C + 5) / 25 - 5;
  57    xor edx, edx
  58    mov eax, C
  59    mov ebx, 8
  60    mul ebx
  61    add eax, 5
  62    mov ebx, 25
  63    div ebx
  64    sub eax, 5
  65    mov Z, eax
  66  
  67    ; int D = 5 * Y / 4 - X - 10;
  68    xor edx, edx
  69    mov eax, Y
  70    mov ebx, 5
  71    mul ebx,
  72    mov ebx, 4
  73    div ebx
  74    sub eax, X
  75    sub eax, 10
  76    mov D, eax
  77  
  78    ; int E = (11 * G + 20 + Z - X) % 30;
  79    mov eax, G
  80    mov ebx, 11
  81    mul ebx
  82    add eax, 20
  83    add eax, Z
  84    sub eax, X
  85    xor edx, edx
  86    mov ebx, 30
  87    div ebx
  88    mov E, edx
  89  
  90    cmp dword E, 24
  91    jne after_e1
  92    inc dword E
  93    jmp after_e2
  94  after_e1:
  95    cmp dword E, 25
  96    jne after_e2
  97    cmp dword G, 11
  98    jle after_e2
  99    inc dword E
 100  after_e2:
 101  
 102    ; int N = 44 - E;
 103    mov eax, 44
 104    sub eax, E
 105    mov N, eax
 106  
 107    cmp dword N, 21
 108    jge after_n
 109    add dword N, 30
 110  after_n:
 111    ; N = N + 7 - ((D + N) % 7);
 112    mov eax, N
 113    add eax, D
 114    xor edx, edx
 115    mov ebx, 7
 116    div ebx
 117    add dword N, 7
 118    sub N, edx
 119  
 120    mov eax, d
 121  
 122    ; N is the nuber of day from March 1 to easter. Check if easter is in April.
 123    cmp dword N, 31
 124    jle in_march
 125    mov bl, N
 126    sub bl, 31
 127    mov [eax], bl
 128    mov word [eax + 2], 4
 129    jmp quit
 130  in_march:
 131    mov bl, N
 132    mov [eax], bl
 133    mov word [eax + 2], 3
 134  quit:
 135    leave
 136    ret

Assembler - Insert sort

This is a straightforward implementation of the insert sort algorithm in assembler (NASM).

Compile and link it with:
nasm -f elf isort.s
ld -s -o isort isort.o

Insert sort: http://en.wikipedia.org/wiki/Insert_sort

   1  
   2  ; void isort(int *a, int n)
   3  ;   sorts the first n elements of a
   4  ;
   5  ; Parameters
   6  ;   a - pointer to the array
   7  ;   n - number of elements to sorts
   8  
   9  %define a [ebp + 8]
  10  %define n [ebp + 12]
  11  isort:
  12    enter 0, 0
  13    pusha
  14  
  15    mov ecx, 1
  16    for:
  17      mov ebx, ecx
  18      imul ebx, 4
  19      add ebx, a
  20      mov ebx, [ebx]
  21  
  22      mov edx, ecx
  23      dec edx
  24  
  25      while:
  26        cmp edx, 0
  27        jl while_quit
  28  
  29        mov eax, edx
  30        imul eax, 4
  31        add eax, a
  32  
  33        cmp ebx, [eax]
  34        jge while_quit
  35  
  36        mov esi, [eax]
  37  
  38        mov dword [eax + 4], esi
  39  
  40        dec edx
  41        jmp while
  42      while_quit:
  43  
  44      mov [eax], ebx
  45  
  46      inc ecx
  47      cmp ecx, n
  48      jl for
  49  
  50    popa
  51    leave
  52    ret
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS