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-4 of 4 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

Easter date - NASM

Compute Easter date in NASM.

segment .text
  global easter_asm

;typedef struct {
;  unsigned short d;
;  unsigned short m;
;} Date;

;void easter_asm(unsigned short Y, Date *d)
;   computes the Easter date for the year Y and stores it in d
;
;Parameters
;   Y - the year
;   d - the Date struct in which to store the result

%define d [ebp + 12]
%define Y [ebp + 8]
easter_asm:
  enter 0, 0
  sub esp, 28
  %define G [ebp - 4]
  %define C [ebp - 8]
  %define X [ebp - 12]
  %define Z [ebp - 16]
  %define D [ebp - 20]
  %define E [ebp - 24]
  %define N [ebp - 28]

  ; int G = (Y % 19) + 1;
  xor edx, edx
  mov eax, Y
  mov ebx, 19
  div ebx
  inc edx
  mov G, edx

  ; int C = (int)(Y / 100) + 1;
  xor edx, edx
  mov eax, Y
  mov ebx, 100
  div ebx
  inc eax
  mov C, eax

  ; int X = 3 * C / 4 - 12;
  xor edx, edx
  mov eax, C
  mov ebx, 4
  div ebx
  mov ebx, 3
  mul ebx
  sub eax, 12
  mov X, eax

  ; int Z = (8 * C + 5) / 25 - 5;
  xor edx, edx
  mov eax, C
  mov ebx, 8
  mul ebx
  add eax, 5
  mov ebx, 25
  div ebx
  sub eax, 5
  mov Z, eax

  ; int D = 5 * Y / 4 - X - 10;
  xor edx, edx
  mov eax, Y
  mov ebx, 5
  mul ebx,
  mov ebx, 4
  div ebx
  sub eax, X
  sub eax, 10
  mov D, eax

  ; int E = (11 * G + 20 + Z - X) % 30;
  mov eax, G
  mov ebx, 11
  mul ebx
  add eax, 20
  add eax, Z
  sub eax, X
  xor edx, edx
  mov ebx, 30
  div ebx
  mov E, edx

  cmp dword E, 24
  jne after_e1
  inc dword E
  jmp after_e2
after_e1:
  cmp dword E, 25
  jne after_e2
  cmp dword G, 11
  jle after_e2
  inc dword E
after_e2:

  ; int N = 44 - E;
  mov eax, 44
  sub eax, E
  mov N, eax

  cmp dword N, 21
  jge after_n
  add dword N, 30
after_n:
  ; N = N + 7 - ((D + N) % 7);
  mov eax, N
  add eax, D
  xor edx, edx
  mov ebx, 7
  div ebx
  add dword N, 7
  sub N, edx

  mov eax, d

  ; N is the nuber of day from March 1 to easter. Check if easter is in April.
  cmp dword N, 31
  jle in_march
  mov bl, N
  sub bl, 31
  mov [eax], bl
  mov word [eax + 2], 4
  jmp quit
in_march:
  mov bl, N
  mov [eax], bl
  mov word [eax + 2], 3
quit:
  leave
  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

; void isort(int *a, int n)
;   sorts the first n elements of a
;
; Parameters
;   a - pointer to the array
;   n - number of elements to sorts

%define a [ebp + 8]
%define n [ebp + 12]
isort:
  enter 0, 0
  pusha

  mov ecx, 1
  for:
    mov ebx, ecx
    imul ebx, 4
    add ebx, a
    mov ebx, [ebx]

    mov edx, ecx
    dec edx

    while:
      cmp edx, 0
      jl while_quit

      mov eax, edx
      imul eax, 4
      add eax, a

      cmp ebx, [eax]
      jge while_quit

      mov esi, [eax]

      mov dword [eax + 4], esi

      dec edx
      jmp while
    while_quit:

    mov [eax], ebx

    inc ecx
    cmp ecx, n
    jl for

  popa
  leave
  ret

Hello assambler

The canonical Hello World program in assambler.

Compile it with:
nasm -f elf hello.s
ld -s -o hello hello.o

Helloworld is a function. It uses the write system call to print to stdout (file descriptor 1).

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.

Download NASM for free at http://nasm.sourceforge.net/

section .data
  hello: db "Hello world!",10
  hellolen: equ $ - hello

section .text
  global _start

_start:
  call helloworld

  mov eax,1 ; start exit procedure
  mov ebx,0

  int 80h

helloworld:
  mov eax,4 ; 4 is the code for the write system call
  mov ebx,1 ; 1 is the fd for stdout
  mov ecx,hello ; the address of the string
  mov edx,hellolen ; the length of the string

  int 80h ; trigger an interrupt; this should be 21h for DOS

  ret
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS