aboutsummaryrefslogtreecommitdiff
path: root/liblang.asm
blob: 52a68cc0404683603c086e2e003214540074a551 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
SYS_EXIT  equ 0x2000001 ;code
SYS_FORK  equ 0x2000002 ;--
SYS_READ  equ 0x2000003 ;fd, buf, len
SYS_WRITE equ 0x2000004 ;fd, buf, len
SYS_MMAP  equ 0x20000C5 ;addr, len, prot, flags, fd, offset

global start, putc, putint, getc, _builtin_malloc
default rel

extern main

section .text
start:
	call main
	mov rdi, rax
	mov eax, SYS_EXIT
	syscall
	jmp $

putc:
	push rax
	push rdi
	push rsi
	push rdx
	push rcx
	push r11
	mov eax, SYS_WRITE
	mov edi, 1
	lea rsi, [rsp+56]
	mov edx, edi
	syscall
	pop r11
	pop rcx
	pop rdx
	pop rsi
	pop rdi
	pop rax
	ret

putint:
	push rdi
	push rsi
	push rax
	push rbx
	push rcx
	push rdx
	push r11
	mov rax, [rsp+64]
	mov ebx, 18
	test rax, rax
	jz .numzero

	mov ecx, 10

.strlp:
	xor edx, edx
	div rcx
	add dl, '0'
	dec rbx
	mov [rsp+rbx], dl
	test rax, rax
	jnz .strlp
	jmp .strdone

.numzero:
	dec rbx
	mov byte [rsp+rbx], '0'
	jmp .strdone

.strdone:
	mov eax, SYS_WRITE
	mov edi, 1
	lea rsi, [rsp+rbx]
	mov edx, 18
	sub rdx, rbx
	syscall

	pop r11
	pop rdx
	pop rcx
	pop rbx
	pop rax
	pop rsi
	pop rdi
	ret

getc:
	push rdi
	push rsi
	push rdx
	push rcx
	push r11
	mov eax, SYS_READ
	xor edi, edi
	mov rsi, rsp
	mov edx, 1
	syscall
	cmp rax, 1
	jne .fail
	mov rax, [rsp]
.finish:
	pop r11
	pop rcx
	pop rdx
	pop rsi
	pop rdi
	ret
.fail:
	mov rax, -1
	jmp .finish

_builtin_malloc:
	push rdi
	push rsi
	push rdx
	push r8
	push r9
	push r10
	push r11
	push rcx
	xor edi, edi
	mov rsi, [rsp+72]
	mov edx, 0x03
	mov r10d, 0x1001
	mov r8d, -1
	xor r9d, r9d
	mov eax, SYS_MMAP
	syscall
	pop rcx
	pop r11
	pop r10
	pop r9
	pop r8
	pop rdx
	pop rsi
	pop rdi
	ret


section .data
db 0  ; for dyld