-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
589 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
void *malloc(int); | ||
void printf(char *, ...); | ||
void sprintf(char *, char *, ...); | ||
|
||
#define PUSHARGS() __asm__("push rdi" \ | ||
"push rsi" \ | ||
"push rdx" \ | ||
"push rcx" \ | ||
"push r8" \ | ||
"push r9") | ||
#define SLARGS() __asm__("mov rdi, rsi" \ | ||
"mov rsi, rdx" \ | ||
"mov rdx, rcx" \ | ||
"mov rcx, r8" \ | ||
"mov r8, r9" \ | ||
"mov r9, 0") | ||
#define SRARGS() __asm__("mov r9, r8" \ | ||
"mov r8, rcx" \ | ||
"mov rcx, rdx" \ | ||
"mov rdx, rsi" \ | ||
"mov rsi, rdi" \ | ||
"mov rdi, 0") | ||
#define POPARGS() __asm__("pop r9" \ | ||
"pop r8" \ | ||
"pop rcx" \ | ||
"pop rdx" \ | ||
"pop rsi" \ | ||
"pop rdi") | ||
|
||
char *cc_asprintf(char *inp, ...) | ||
{ | ||
SRARGS(); | ||
PUSHARGS(); | ||
char *ret = (char *)malloc(128); | ||
POPARGS(); | ||
sprintf(ret, inp); | ||
return ret; | ||
} | ||
|
||
int main() | ||
{ | ||
char *ret = cc_asprintf("salam %s %u%c", "donya", 123, '!'); | ||
printf("%s %s\n", "haha", ret); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
section .data | ||
__cc_asprintf_size: equ 112 | ||
__temp_str_1 db `salam %s %u%c`, 0 | ||
__temp_str_2 db `donya`, 0 | ||
__temp_str_3 db `%s %s\n`, 0 | ||
__temp_str_4 db `haha`, 0 | ||
__main_size: equ 96 | ||
section .text | ||
extern malloc | ||
extern printf | ||
extern sprintf | ||
global cc_asprintf | ||
cc_asprintf: | ||
push rbp | ||
mov rbp, rsp | ||
sub rsp, __cc_asprintf_size | ||
mov [rbp-8], rdi | ||
mov r9, r8 | ||
mov r8, rcx | ||
mov rcx, rdx | ||
mov rdx, rsi | ||
mov rsi, rdi | ||
mov rdi, 0 | ||
push rdi | ||
push rsi | ||
push rdx | ||
push rcx | ||
push r8 | ||
push r9 | ||
mov rax, 128 | ||
mov [rbp-24], rax | ||
mov rdi, [rbp-24] | ||
mov rax, rbp | ||
sub rax, 8 | ||
mov [rbp-32], rax | ||
call malloc | ||
mov [rbp-40], rax | ||
xor rax, rax | ||
mov [rbp-48], rax | ||
mov rax, [rbp-40] | ||
mov [rbp-48], rax | ||
;define variable ret | ||
mov rax, [rbp-48] | ||
mov [rbp-16], rax | ||
;end define variable ret | ||
pop r9 | ||
pop r8 | ||
pop rcx | ||
pop rdx | ||
pop rsi | ||
pop rdi | ||
mov rax, rbp | ||
sub rax, 16 | ||
mov [rbp-56], rax | ||
mov rax, [rbp-16] | ||
mov [rbp-64], rax | ||
mov rax, rbp | ||
sub rax, 8 | ||
mov [rbp-72], rax | ||
mov rax, [rbp-8] | ||
mov [rbp-80], rax | ||
mov rdi, [rbp-64] | ||
mov rsi, [rbp-80] | ||
mov rax, rbp | ||
sub rax, 8 | ||
mov [rbp-88], rax | ||
call sprintf | ||
mov rax, rbp | ||
sub rax, 16 | ||
mov [rbp-96], rax | ||
mov rax, [rbp-16] | ||
mov rsp, rbp | ||
pop rbp | ||
ret | ||
mov rsp, rbp | ||
pop rbp | ||
ret | ||
global main | ||
main: | ||
push rbp | ||
mov rbp, rsp | ||
sub rsp, __main_size | ||
mov rax, __temp_str_1 | ||
mov [rbp-16], rax | ||
mov rax, __temp_str_2 | ||
mov [rbp-24], rax | ||
mov rax, 123 | ||
mov [rbp-32], rax | ||
mov al, 33 | ||
mov [rbp-33], al | ||
mov rdi, [rbp-16] | ||
mov rsi, [rbp-24] | ||
mov rdx, [rbp-32] | ||
mov rcx, [rbp-33] | ||
mov rax, rbp | ||
sub rax, 8 | ||
mov [rbp-41], rax | ||
call cc_asprintf | ||
mov [rbp-49], rax | ||
;define variable ret | ||
mov rax, [rbp-49] | ||
mov [rbp-8], rax | ||
;end define variable ret | ||
mov rax, __temp_str_3 | ||
mov [rbp-57], rax | ||
mov rax, __temp_str_4 | ||
mov [rbp-65], rax | ||
mov rax, rbp | ||
sub rax, 8 | ||
mov [rbp-73], rax | ||
mov rax, [rbp-8] | ||
mov [rbp-81], rax | ||
mov rdi, [rbp-57] | ||
mov rsi, [rbp-65] | ||
mov rdx, [rbp-81] | ||
mov rax, rbp | ||
sub rax, 8 | ||
mov [rbp-89], rax | ||
call printf | ||
mov rax, 0 | ||
mov rsp, rbp | ||
pop rbp | ||
ret | ||
mov rsp, rbp | ||
pop rbp | ||
ret | ||
extern exit | ||
global _start | ||
_start: | ||
; Pass argc and argv | ||
mov rdi, [rsp] | ||
mov rsi, rsp | ||
add rsi, 8 | ||
call main | ||
mov rdi, rax | ||
call exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
TKN_VOID | ||
TKN_STAR | ||
TKN_ID(malloc) | ||
TKN_L_PAREN | ||
TKN_INT | ||
TKN_R_PAREN | ||
TKN_SEMICOLON | ||
TKN_VOID | ||
TKN_ID(printf) | ||
TKN_L_PAREN | ||
TKN_CHAR | ||
TKN_STAR | ||
TKN_COMMA | ||
TKN_DOTS | ||
TKN_R_PAREN | ||
TKN_SEMICOLON | ||
TKN_VOID | ||
TKN_ID(sprintf) | ||
TKN_L_PAREN | ||
TKN_CHAR | ||
TKN_STAR | ||
TKN_COMMA | ||
TKN_CHAR | ||
TKN_STAR | ||
TKN_COMMA | ||
TKN_DOTS | ||
TKN_R_PAREN | ||
TKN_SEMICOLON | ||
TKN_DIRECTIVE: | ||
TKN_ID(define) | ||
TKN_ID(PUSHARGS) | ||
TKN_L_PAREN | ||
TKN_R_PAREN | ||
TKN_ASM | ||
TKN_L_PAREN | ||
TKN_LIT_STR(push rdi) | ||
TKN_LIT_STR(push rsi) | ||
TKN_LIT_STR(push rdx) | ||
TKN_LIT_STR(push rcx) | ||
TKN_LIT_STR(push r8) | ||
TKN_LIT_STR(push r9) | ||
TKN_R_PAREN | ||
TKN_EOF | ||
TKN_DIRECTIVE: | ||
TKN_ID(define) | ||
TKN_ID(SLARGS) | ||
TKN_L_PAREN | ||
TKN_R_PAREN | ||
TKN_ASM | ||
TKN_L_PAREN | ||
TKN_LIT_STR(mov rdi, rsi) | ||
TKN_LIT_STR(mov rsi, rdx) | ||
TKN_LIT_STR(mov rdx, rcx) | ||
TKN_LIT_STR(mov rcx, r8) | ||
TKN_LIT_STR(mov r8, r9) | ||
TKN_LIT_STR(mov r9, 0) | ||
TKN_R_PAREN | ||
TKN_EOF | ||
TKN_DIRECTIVE: | ||
TKN_ID(define) | ||
TKN_ID(SRARGS) | ||
TKN_L_PAREN | ||
TKN_R_PAREN | ||
TKN_ASM | ||
TKN_L_PAREN | ||
TKN_LIT_STR(mov r9, r8) | ||
TKN_LIT_STR(mov r8, rcx) | ||
TKN_LIT_STR(mov rcx, rdx) | ||
TKN_LIT_STR(mov rdx, rsi) | ||
TKN_LIT_STR(mov rsi, rdi) | ||
TKN_LIT_STR(mov rdi, 0) | ||
TKN_R_PAREN | ||
TKN_EOF | ||
TKN_DIRECTIVE: | ||
TKN_ID(define) | ||
TKN_ID(POPARGS) | ||
TKN_L_PAREN | ||
TKN_R_PAREN | ||
TKN_ASM | ||
TKN_L_PAREN | ||
TKN_LIT_STR(pop r9) | ||
TKN_LIT_STR(pop r8) | ||
TKN_LIT_STR(pop rcx) | ||
TKN_LIT_STR(pop rdx) | ||
TKN_LIT_STR(pop rsi) | ||
TKN_LIT_STR(pop rdi) | ||
TKN_R_PAREN | ||
TKN_EOF | ||
TKN_CHAR | ||
TKN_STAR | ||
TKN_ID(cc_asprintf) | ||
TKN_L_PAREN | ||
TKN_CHAR | ||
TKN_STAR | ||
TKN_ID(inp) | ||
TKN_COMMA | ||
TKN_DOTS | ||
TKN_R_PAREN | ||
TKN_L_BRACE | ||
TKN_ID(SRARGS) | ||
TKN_L_PAREN | ||
TKN_R_PAREN | ||
TKN_SEMICOLON | ||
TKN_ID(PUSHARGS) | ||
TKN_L_PAREN | ||
TKN_R_PAREN | ||
TKN_SEMICOLON | ||
TKN_CHAR | ||
TKN_STAR | ||
TKN_ID(ret) | ||
TKN_ASSIGN | ||
TKN_L_PAREN | ||
TKN_CHAR | ||
TKN_STAR | ||
TKN_R_PAREN | ||
TKN_ID(malloc) | ||
TKN_L_PAREN | ||
TKN_LIT_INT(128) | ||
TKN_R_PAREN | ||
TKN_SEMICOLON | ||
TKN_ID(POPARGS) | ||
TKN_L_PAREN | ||
TKN_R_PAREN | ||
TKN_SEMICOLON | ||
TKN_ID(sprintf) | ||
TKN_L_PAREN | ||
TKN_ID(ret) | ||
TKN_COMMA | ||
TKN_ID(inp) | ||
TKN_R_PAREN | ||
TKN_SEMICOLON | ||
TKN_RETURN | ||
TKN_ID(ret) | ||
TKN_SEMICOLON | ||
TKN_R_BRACE | ||
TKN_INT | ||
TKN_ID(main) | ||
TKN_L_PAREN | ||
TKN_R_PAREN | ||
TKN_L_BRACE | ||
TKN_CHAR | ||
TKN_STAR | ||
TKN_ID(ret) | ||
TKN_ASSIGN | ||
TKN_ID(cc_asprintf) | ||
TKN_L_PAREN | ||
TKN_LIT_STR(salam %s %u%c) | ||
TKN_COMMA | ||
TKN_LIT_STR(donya) | ||
TKN_COMMA | ||
TKN_LIT_INT(123) | ||
TKN_COMMA | ||
TKN_LIT_CHAR(!) | ||
TKN_R_PAREN | ||
TKN_SEMICOLON | ||
TKN_ID(printf) | ||
TKN_L_PAREN | ||
TKN_LIT_STR(%s %s | ||
) | ||
TKN_COMMA | ||
TKN_LIT_STR(haha) | ||
TKN_COMMA | ||
TKN_ID(ret) | ||
TKN_R_PAREN | ||
TKN_SEMICOLON | ||
TKN_RETURN | ||
TKN_LIT_INT(0) | ||
TKN_SEMICOLON | ||
TKN_R_BRACE | ||
TKN_EOF |
Oops, something went wrong.