-
Notifications
You must be signed in to change notification settings - Fork 0
/
kc5-mac.c
78 lines (66 loc) · 1.57 KB
/
kc5-mac.c
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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char *argv[]) {
if(argc != 2) {
fprintf(stderr, "illegal number of arguments\n");
return 1;
}
char *p = argv[1];
puts(".intel_syntax noprefix");
puts(".globl _main");
puts("_main:");
while(*p) {
if(isspace(*p)) {
p++;
continue;
}
if(*p == '+') {
puts(" pop rdi");
puts(" pop rax");
puts(" add rax, rdi");
puts(" push rax");
p++;
continue;
}
if(*p == '-') {
puts(" pop rdi");
puts(" pop rax");
puts(" sub rax, rdi");
puts(" push rax");
p++;
continue;
}
if(*p == '*') {
puts(" pop rdi");
puts(" pop rax");
puts(" imul rax, rdi");
puts(" push rax");
p++;
continue;
}
if(*p == '/') {
puts(" pop rdi");
puts(" pop rax");
puts(" cqo");
puts(" idiv rdi");
puts(" push rax");
p++;
continue;
}
if(isdigit(*p)) {
int n = 0;
while(isdigit(*p)) {
n = n * 10 + (*p - '0');
p++;
}
printf(" push %d\n", n);
continue;
}
fprintf(stderr, "unexpected character: '%c'\n", *p);
return 1;
}
puts(" pop rax");
puts(" ret");
return 0;
}