Skip to content

Instantly share code, notes, and snippets.

@martinlindhe
Created December 3, 2015 09:58
Show Gist options
  • Save martinlindhe/b92227022a99172766a2 to your computer and use it in GitHub Desktop.
Save martinlindhe/b92227022a99172766a2 to your computer and use it in GitHub Desktop.
advent of code 1.1 in BSD (osx) assembly
section .text
global mystart
int_to_string:
add esi,9
mov byte [esi],0
mov ebx,10
.next:
xor edx,edx
div ebx
add dl,'0'
dec esi
mov [esi],dl
test eax,eax
jnz .next
mov eax,esi
ret
mystart:
mov dword [cnt], 0
mov ecx, s1
loop1:
mov dh, [ecx]
cmp dh, 0
jz done
cmp dh, '('
jz inc1
dec dword [cnt]
jmp cont
inc1:
inc dword [cnt]
cont:
inc ecx
jmp loop1
done:
mov eax, [cnt]
mov esi, buff
call int_to_string
push dword 10
push dword buff
push dword 1
mov eax, 0x4
sub esp, 4
int 0x80
add esp, 16
push dword 0
mov eax, 0x1
sub esp, 4
int 0x80
section .data
s1 db "(())", 0
s1len equ $-s1
cnt dw 0
buff db ' '
#!/bin/sh
# build
nasm -f macho main.asm
# link
ld -o hello -e mystart main.o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment