Get rid of unneeded function prologues/epilogues
This commit is contained in:
10
console.asm
10
console.asm
@ -19,24 +19,16 @@ section .text
|
|||||||
;----- print (char* string) -----;
|
;----- print (char* string) -----;
|
||||||
; return value: N/A
|
; return value: N/A
|
||||||
print:
|
print:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
call strlen
|
call strlen
|
||||||
mov rdx, rax
|
mov rdx, rax
|
||||||
mov rax, NR_write
|
mov rax, NR_write
|
||||||
mov rsi, rdi
|
mov rsi, rdi
|
||||||
mov rdi, 1
|
mov rdi, 1
|
||||||
syscall
|
syscall
|
||||||
|
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- puts (char* string) -----;
|
;----- puts (char* string) -----;
|
||||||
; return value: N/A
|
; return value: N/A
|
||||||
puts:
|
puts:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
mov r10, rdi
|
mov r10, rdi
|
||||||
call print
|
call print
|
||||||
mov rdi, r10
|
mov rdi, r10
|
||||||
@ -45,8 +37,6 @@ puts:
|
|||||||
mov rsi, NL
|
mov rsi, NL
|
||||||
mov rdx, 1
|
mov rdx, 1
|
||||||
syscall
|
syscall
|
||||||
|
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- printf(const char* string, ...) -----;
|
;----- printf(const char* string, ...) -----;
|
||||||
; Currently only supports specifiers: %d, %c, %s, %%
|
; Currently only supports specifiers: %d, %c, %s, %%
|
||||||
|
44
core.asm
44
core.asm
@ -16,121 +16,78 @@ section .text
|
|||||||
;----- min(int a, int b) -----;
|
;----- min(int a, int b) -----;
|
||||||
; return value: lowest value
|
; return value: lowest value
|
||||||
min:
|
min:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
mov rax, rdi
|
mov rax, rdi
|
||||||
cmp rdi, rsi
|
cmp rdi, rsi
|
||||||
cmovg rax, rsi
|
cmovg rax, rsi
|
||||||
|
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- minu(unsigned int a, unsigned int b) -----;
|
;----- minu(unsigned int a, unsigned int b) -----;
|
||||||
; return value: lowest value
|
; return value: lowest value
|
||||||
minu:
|
minu:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
mov rax, rdi
|
mov rax, rdi
|
||||||
cmp rdi, rsi
|
cmp rdi, rsi
|
||||||
cmova rax, rsi
|
cmova rax, rsi
|
||||||
|
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- max(int a, int b) -----;
|
;----- max(int a, int b) -----;
|
||||||
; return value: highest value
|
; return value: highest value
|
||||||
max:
|
max:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
mov rax, rdi
|
mov rax, rdi
|
||||||
cmp rdi, rsi
|
cmp rdi, rsi
|
||||||
cmovl rax, rsi
|
cmovl rax, rsi
|
||||||
|
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- maxu(unsigned int a, unsigned int b) -----;
|
;----- maxu(unsigned int a, unsigned int b) -----;
|
||||||
; return value: highest value
|
; return value: highest value
|
||||||
maxu:
|
maxu:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
mov rax, rdi
|
mov rax, rdi
|
||||||
cmp rdi, rsi
|
cmp rdi, rsi
|
||||||
cmovb rax, rsi
|
cmovb rax, rsi
|
||||||
|
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- islower(char* c) -----;
|
;----- islower(char* c) -----;
|
||||||
; return value: 1 (character is in range of 'a'-'z') or 0 (character is nog in range of 'a'-'z')
|
; return value: 1 (character is in range of 'a'-'z') or 0 (character is nog in range of 'a'-'z')
|
||||||
islower:
|
islower:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
cmp byte [rdi], 'a'
|
cmp byte [rdi], 'a'
|
||||||
jb .quit
|
jb .quit
|
||||||
cmp byte [rdi], 'z'
|
cmp byte [rdi], 'z'
|
||||||
ja .quit
|
ja .quit
|
||||||
mov rax, 1
|
mov rax, 1
|
||||||
|
|
||||||
.quit:
|
.quit:
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- isupper(char* c) -----;
|
;----- isupper(char* c) -----;
|
||||||
; return value: 1 (character is in range of 'A'-'Z') or 0 (character is not in range of 'A'-'Z')
|
; return value: 1 (character is in range of 'A'-'Z') or 0 (character is not in range of 'A'-'Z')
|
||||||
isupper:
|
isupper:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
cmp byte [rdi], 'A'
|
cmp byte [rdi], 'A'
|
||||||
jb .quit
|
jb .quit
|
||||||
cmp byte [rdi], 'Z'
|
cmp byte [rdi], 'Z'
|
||||||
ja .quit
|
ja .quit
|
||||||
mov rax, 1
|
mov rax, 1
|
||||||
|
|
||||||
.quit:
|
.quit:
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- tolower(char* c) -----;
|
;----- tolower(char* c) -----;
|
||||||
; return value: ASCII value. Same as original if it was not an uppercase letter, otherwise lowercase letter
|
; return value: ASCII value. Same as original if it was not an uppercase letter, otherwise lowercase letter
|
||||||
tolower:
|
tolower:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
call isupper
|
call isupper
|
||||||
mov rcx, rax
|
mov rcx, rax
|
||||||
mov al, byte [rdi]
|
mov al, byte [rdi]
|
||||||
cmp rcx, 1
|
cmp rcx, 1
|
||||||
jne .quit
|
jne .quit
|
||||||
add al, 32
|
add al, 32
|
||||||
|
|
||||||
.quit:
|
.quit:
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- toupper(char* c) -----;
|
;----- toupper(char* c) -----;
|
||||||
; return value: ASCII value. Same as original if it was not a lowercase letter, otherwise uppercase letter
|
; return value: ASCII value. Same as original if it was not a lowercase letter, otherwise uppercase letter
|
||||||
toupper:
|
toupper:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
call islower
|
call islower
|
||||||
mov rcx, rax
|
mov rcx, rax
|
||||||
mov al, byte [rdi]
|
mov al, byte [rdi]
|
||||||
cmp rcx, 1
|
cmp rcx, 1
|
||||||
jne .quit
|
jne .quit
|
||||||
sub al, 32
|
sub al, 32
|
||||||
|
|
||||||
.quit:
|
.quit:
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- itoa(int num, char* str) -----;
|
;----- itoa(int num, char* str) -----;
|
||||||
; return value: pointer to str
|
; return value: pointer to str
|
||||||
itoa:
|
itoa:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
push rbx
|
push rbx
|
||||||
push rsi
|
push rsi
|
||||||
push rdi
|
push rdi
|
||||||
@ -178,7 +135,6 @@ itoa:
|
|||||||
.quit:
|
.quit:
|
||||||
pop rax
|
pop rax
|
||||||
pop rbx
|
pop rbx
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- exit(int exit_code) -----;
|
;----- exit(int exit_code) -----;
|
||||||
; returns: nothing
|
; returns: nothing
|
||||||
|
12
file.asm
12
file.asm
@ -21,9 +21,6 @@ section .text
|
|||||||
;----- fopen(const char *filename, const char mode) -----;
|
;----- fopen(const char *filename, const char mode) -----;
|
||||||
; return value: pointer to file or value < 0 (neg errno) if file failed to open
|
; return value: pointer to file or value < 0 (neg errno) if file failed to open
|
||||||
fopen:
|
fopen:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
cmp sil, 'r'
|
cmp sil, 'r'
|
||||||
je .setMode_r
|
je .setMode_r
|
||||||
cmp sil, 'w'
|
cmp sil, 'w'
|
||||||
@ -63,14 +60,10 @@ fopen:
|
|||||||
syscall
|
syscall
|
||||||
|
|
||||||
.quit:
|
.quit:
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- fclose(FILE* fp) -----;
|
;----- fclose(FILE* fp) -----;
|
||||||
; return value: 0 on success, below 0 otherwise (neg errno)
|
; return value: 0 on success, below 0 otherwise (neg errno)
|
||||||
fclose:
|
fclose:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
cmp rdi, 3
|
cmp rdi, 3
|
||||||
jl .ebadf
|
jl .ebadf
|
||||||
|
|
||||||
@ -82,15 +75,11 @@ fclose:
|
|||||||
mov rax, -9
|
mov rax, -9
|
||||||
|
|
||||||
.quit:
|
.quit:
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- fwrite(FILE* fp, char* str) -----;
|
;----- fwrite(FILE* fp, char* str) -----;
|
||||||
; writes a string to opened file
|
; writes a string to opened file
|
||||||
; return value: amount of written characters on success, below 0 otherwise (neg errno)
|
; return value: amount of written characters on success, below 0 otherwise (neg errno)
|
||||||
fwrite:
|
fwrite:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
cmp rdi, 3
|
cmp rdi, 3
|
||||||
jl .ebadf
|
jl .ebadf
|
||||||
|
|
||||||
@ -107,5 +96,4 @@ fwrite:
|
|||||||
mov rax, -9
|
mov rax, -9
|
||||||
|
|
||||||
.quit:
|
.quit:
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
|
43
string.asm
43
string.asm
@ -10,9 +10,6 @@ section .text
|
|||||||
;----- strlen(char* str) -----;
|
;----- strlen(char* str) -----;
|
||||||
; return value: amount of characters before EOS was found
|
; return value: amount of characters before EOS was found
|
||||||
strlen:
|
strlen:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
.findEOS:
|
.findEOS:
|
||||||
cmp byte [rdi+rax], 0x0
|
cmp byte [rdi+rax], 0x0
|
||||||
@ -21,14 +18,10 @@ strlen:
|
|||||||
jmp .findEOS
|
jmp .findEOS
|
||||||
|
|
||||||
.quit:
|
.quit:
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- strcpy(char* dest, char* src) -----;
|
;----- strcpy(char* dest, char* src) -----;
|
||||||
; return value: pointer to dest or NULL if nothing was copied (note that EOS is always copied to dest)
|
; return value: pointer to dest or NULL if nothing was copied (note that EOS is always copied to dest)
|
||||||
strcpy:
|
strcpy:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
mov r11, rdi
|
mov r11, rdi
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
xor rcx, rcx
|
xor rcx, rcx
|
||||||
@ -49,14 +42,10 @@ strcpy:
|
|||||||
mov rax, rdi
|
mov rax, rdi
|
||||||
|
|
||||||
.quit:
|
.quit:
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- strlcpy(char* dest, char* src, int size) -----;
|
;----- strlcpy(char* dest, char* src, int size) -----;
|
||||||
; return value: Length of src
|
; return value: Length of src
|
||||||
strlcpy:
|
strlcpy:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
mov r9, rsi
|
mov r9, rsi
|
||||||
test rdx, rdx
|
test rdx, rdx
|
||||||
jz .quit
|
jz .quit
|
||||||
@ -79,29 +68,20 @@ strlcpy:
|
|||||||
.quit:
|
.quit:
|
||||||
mov rdi, r9
|
mov rdi, r9
|
||||||
call strlen
|
call strlen
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- strcat(char* dest, char* src) -----;
|
;----- strcat(char* dest, char* src) -----;
|
||||||
; return value: pointer to dest
|
; return value: pointer to dest
|
||||||
strcat:
|
strcat:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
mov r8, rdi
|
mov r8, rdi
|
||||||
call strlen
|
call strlen
|
||||||
add rdi, rax
|
add rdi, rax
|
||||||
call strcpy
|
call strcpy
|
||||||
mov rax, r8
|
mov rax, r8
|
||||||
|
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- strcmp(char* str1, char* str2) -----;
|
;----- strcmp(char* str1, char* str2) -----;
|
||||||
; return value: 0 if both strings are the same, otherwise index in array str1 where strings mismatch
|
; return value: 0 if both strings are the same, otherwise index in array str1 where strings mismatch
|
||||||
; returns -1 if str2 is longer than str1 and no difference was found before that happens
|
; returns -1 if str2 is longer than str1 and no difference was found before that happens
|
||||||
strcmp:
|
strcmp:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
call strlen
|
call strlen
|
||||||
xor r10, r10
|
xor r10, r10
|
||||||
.compareLoop:
|
.compareLoop:
|
||||||
@ -115,25 +95,21 @@ strcmp:
|
|||||||
inc r10
|
inc r10
|
||||||
jmp .compareLoop
|
jmp .compareLoop
|
||||||
.0f:
|
.0f:
|
||||||
mov rax, -1
|
mov rax, -1
|
||||||
cmp byte [rsi], 0x0
|
cmp byte [rsi], 0x0
|
||||||
jne .quit
|
jne .quit
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
jmp .quit
|
jmp .quit
|
||||||
|
|
||||||
.mismatch:
|
.mismatch:
|
||||||
mov rax, r10
|
mov rax, r10
|
||||||
|
|
||||||
.quit:
|
.quit:
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- strclr(char* str) -----;
|
;----- strclr(char* str) -----;
|
||||||
; Replaces all characters of a string with \0 untill EOS is found
|
; Replaces all characters of a string with \0 untill EOS is found
|
||||||
; returns amount of changed characters
|
; returns amount of changed characters
|
||||||
strclr:
|
strclr:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
call strlen
|
call strlen
|
||||||
mov rcx, rax
|
mov rcx, rax
|
||||||
mov rdx, rax
|
mov rdx, rax
|
||||||
@ -141,21 +117,14 @@ strclr:
|
|||||||
cld
|
cld
|
||||||
rep stosb
|
rep stosb
|
||||||
mov rax, rdx
|
mov rax, rdx
|
||||||
|
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
;----- strlclr(char* str, int size) -----;
|
;----- strlclr(char* str, int size) -----;
|
||||||
; Replaces characters of a string with \0, using arg size
|
; Replaces characters of a string with \0, using arg size
|
||||||
; <!> DANGEROUS FUNCTION, it might exceed string boundaries if used incorrectly. Use strclr() instead!
|
; <!> DANGEROUS FUNCTION, it might exceed string boundaries if used incorrectly. Use strclr() instead!
|
||||||
; Function has no return value, rax will be 0 (0x0 stored in rax for opcode stosb)
|
; Function has no return value, rax will be 0 (0x0 stored in rax for opcode stosb)
|
||||||
strlclr:
|
strlclr:
|
||||||
push rbp
|
|
||||||
mov rbp, rsp
|
|
||||||
|
|
||||||
mov rcx, rsi
|
mov rcx, rsi
|
||||||
xor rax, rax
|
xor rax, rax
|
||||||
cld
|
cld
|
||||||
rep stosb
|
rep stosb
|
||||||
|
|
||||||
leave
|
|
||||||
ret
|
ret
|
||||||
|
Reference in New Issue
Block a user