Skip to content

Commit

Permalink
Switch to GAS style x86 defines.
Browse files Browse the repository at this point in the history
  • Loading branch information
stikonas committed Dec 2, 2022
1 parent 48629f1 commit c95bc8d
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 276 deletions.
32 changes: 16 additions & 16 deletions x86/libc-core.M1
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@

:_start

COPY_esp_to_ebp ; Protect esp
mov_ebp,esp ; Protect esp

;; Prepare argv
LOAD_BASE_ADDRESS_eax %4 ; ARGV_address = EBP + 4
PUSH_eax ; Put argv on the stack
lea_eax,[ebp+DWORD] %4 ; ARGV_address = EBP + 4
push_eax ; Put argv on the stack

;; Prepare envp
COPY_ebp_to_eax ; Address we need to load from
LOAD_INTEGER ; Get ARGC
ADD_IMMEDIATE_to_eax %2 ; OFFSET = ARGC + 2
SAL_eax_Immediate8 !2 ; OFFSET = OFFSET * WORDSIZE
ADD_ebp_to_eax ; ENVP_address = ESP + OFFSET
PUSH_eax ; Put envp on the stack
mov_eax,ebp ; Address we need to load from
mov_eax,[eax] ; Get ARGC
add_eax, %2 ; OFFSET = ARGC + 2
sal_eax, !2 ; OFFSET = OFFSET * WORDSIZE
add_eax,ebp ; ENVP_address = ESP + OFFSET
push_eax ; Put envp on the stack

;; Stack offset
ADD_IMMEDIATE_to_ebp %4 ; Fix ebp
add_ebp, %4 ; Fix ebp

;; Perform the main loop
CALL_IMMEDIATE %FUNCTION_main
PUSH_eax ; Put return on stack
PUSH_eax ; so that _exit gets the value
call %FUNCTION_main
push_eax ; Put return on stack
push_eax ; so that _exit gets the value

;; Exit to kernel
:FUNCTION_exit
:FUNCTION__exit
COPY_eax_to_ebx ; Using the return code given by main
LOAD_IMMEDIATE_eax %1 ; Syscall exit
INT_80 ; Exit with that code
mov_ebx,eax ; Using the return code given by main
mov_eax, %1 ; Syscall exit
int !0x80 ; Exit with that code
40 changes: 20 additions & 20 deletions x86/libc-full.M1
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,38 @@

:_start

COPY_esp_to_ebp ; Protect esp
mov_ebp,esp ; Protect esp

;; Prepare argv
LOAD_BASE_ADDRESS_eax %4 ; ARGV_address = EBP + 4
PUSH_eax ; Put argv on the stack
lea_eax,[ebp+DWORD] %4 ; ARGV_address = EBP + 4
push_eax ; Put argv on the stack

;; Prepare envp
COPY_ebp_to_eax ; Address we need to load from
LOAD_INTEGER ; Get ARGC
ADD_IMMEDIATE_to_eax %2 ; OFFSET = ARGC + 2
SAL_eax_Immediate8 !2 ; OFFSET = OFFSET * WORDSIZE
ADD_ebp_to_eax ; ENVP_address = ESP + OFFSET
PUSH_eax ; Put envp on the stack
mov_eax,ebp ; Address we need to load from
mov_eax,[eax] ; Get ARGC
add_eax, %2 ; OFFSET = ARGC + 2
sal_eax, !2 ; OFFSET = OFFSET * WORDSIZE
add_eax,ebp ; ENVP_address = ESP + OFFSET
push_eax ; Put envp on the stack

;; Stack offset
ADD_IMMEDIATE_to_ebp %4 ; Fix ebp
add_ebp, %4 ; Fix ebp

;; Setup for malloc
CALL_IMMEDIATE %FUNCTION___init_malloc
call %FUNCTION___init_malloc

;; Setup for FILE*
CALL_IMMEDIATE %FUNCTION___init_io
call %FUNCTION___init_io

;; Perform the main loop
CALL_IMMEDIATE %FUNCTION_main
PUSH_eax ; Put return on stack
PUSH_eax ; so that _exit gets the value
call %FUNCTION_main
push_eax ; Put return on stack
push_eax ; so that _exit gets the value

:FUNCTION_exit
CALL_IMMEDIATE %FUNCTION___kill_io
call %FUNCTION___kill_io
:FUNCTION__exit
POP_ebx
POP_ebx
LOAD_IMMEDIATE_eax %1
INT_80
pop_ebx
pop_ebx
mov_eax, %1
int !0x80
76 changes: 38 additions & 38 deletions x86/linux/bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,28 @@

int fgetc(FILE* f)
{
asm("LOAD_IMMEDIATE_eax %3"
"LOAD_EFFECTIVE_ADDRESS_ebx %4"
"LOAD_INTEGER_ebx"
"PUSH_ebx"
"COPY_esp_to_ecx"
"LOAD_IMMEDIATE_edx %1"
"INT_80"
"TEST"
"POP_eax"
"JUMP_NE8 !FUNCTION_fgetc_Done"
"LOAD_IMMEDIATE_eax %-1"
asm("mov_eax, %3"
"lea_ebx,[esp+DWORD] %4"
"mov_ebx,[ebx]"
"push_ebx"
"mov_ecx,esp"
"mov_edx, %1"
"int !0x80"
"test_eax,eax"
"pop_eax"
"jne %FUNCTION_fgetc_Done"
"mov_eax, %-1"
":FUNCTION_fgetc_Done");
}

void fputc(char s, FILE* f)
{
asm("LOAD_IMMEDIATE_eax %4"
"LOAD_EFFECTIVE_ADDRESS_ebx %4"
"LOAD_INTEGER_ebx"
"LOAD_EFFECTIVE_ADDRESS_ecx %8"
"LOAD_IMMEDIATE_edx %1"
"INT_80");
asm("mov_eax, %4"
"lea_ebx,[esp+DWORD] %4"
"mov_ebx,[ebx]"
"lea_ecx,[esp+DWORD] %8"
"mov_edx, %1"
"int !0x80");
}

void fputs(char* s, FILE* f)
Expand All @@ -63,14 +63,14 @@ void fputs(char* s, FILE* f)

FILE* open(char* name, int flag, int mode)
{
asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
"LOAD_INTEGER_ebx"
"LOAD_EFFECTIVE_ADDRESS_ecx %8"
"LOAD_INTEGER_ecx"
"LOAD_EFFECTIVE_ADDRESS_edx %4"
"LOAD_INTEGER_edx"
"LOAD_IMMEDIATE_eax %5"
"INT_80");
asm("lea_ebx,[esp+DWORD] %12"
"mov_ebx,[ebx]"
"lea_ecx,[esp+DWORD] %8"
"mov_ecx,[ecx]"
"lea_edx,[esp+DWORD] %4"
"mov_edx,[edx]"
"mov_eax, %5"
"int !0x80");
}

FILE* fopen(char* filename, char* mode)
Expand All @@ -95,10 +95,10 @@ FILE* fopen(char* filename, char* mode)

int close(int fd)
{
asm("LOAD_EFFECTIVE_ADDRESS_ebx %4"
"LOAD_INTEGER_ebx"
"LOAD_IMMEDIATE_eax %6"
"INT_80");
asm("lea_ebx,[esp+DWORD] %4"
"mov_ebx,[ebx]"
"mov_eax, %6"
"int !0x80");
}

int fclose(FILE* stream)
Expand All @@ -109,11 +109,11 @@ int fclose(FILE* stream)

int brk(void *addr)
{
asm("LOAD_ESP_IMMEDIATE_into_eax %4"
"PUSH_eax"
"LOAD_IMMEDIATE_eax %45"
"POP_ebx"
"INT_80");
asm("mov_eax,[esp+DWORD] %4"
"push_eax"
"mov_eax, %45"
"pop_ebx"
"int !0x80");
}

long _malloc_ptr;
Expand Down Expand Up @@ -170,8 +170,8 @@ void free(void* l)

void exit(int value)
{
asm("POP_ebx"
"POP_ebx"
"LOAD_IMMEDIATE_eax %1"
"INT_80");
asm("pop_ebx"
"pop_ebx"
"mov_eax, %1"
"int !0x80");
}
16 changes: 8 additions & 8 deletions x86/linux/fcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@

int _open(char* name, int flag, int mode)
{
asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
"LOAD_INTEGER_ebx"
"LOAD_EFFECTIVE_ADDRESS_ecx %8"
"LOAD_INTEGER_ecx"
"LOAD_EFFECTIVE_ADDRESS_edx %4"
"LOAD_INTEGER_edx"
"LOAD_IMMEDIATE_eax %5"
"INT_80");
asm("lea_ebx,[esp+DWORD] %12"
"mov_ebx,[ebx]"
"lea_ecx,[esp+DWORD] %8"
"mov_ecx,[ecx]"
"lea_edx,[esp+DWORD] %4"
"mov_edx,[edx]"
"mov_eax, %5"
"int !0x80");
}

#define STDIN_FILENO 0
Expand Down
60 changes: 30 additions & 30 deletions x86/linux/sys/stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,56 +39,56 @@

int chmod(char *pathname, int mode)
{
asm("LOAD_EFFECTIVE_ADDRESS_ebx %8"
"LOAD_INTEGER_ebx"
"LOAD_EFFECTIVE_ADDRESS_ecx %4"
"LOAD_INTEGER_ecx"
"LOAD_IMMEDIATE_eax %15"
"INT_80");
asm("lea_ebx,[esp+DWORD] %8"
"mov_ebx,[ebx]"
"lea_ecx,[esp+DWORD] %4"
"mov_ecx,[ecx]"
"mov_eax, %15"
"int !0x80");
}


int fchmod(int a, mode_t b)
{
asm("LOAD_EFFECTIVE_ADDRESS_ebx %8"
"LOAD_INTEGER_ebx"
"LOAD_EFFECTIVE_ADDRESS_ecx %4"
"LOAD_INTEGER_ecx"
"LOAD_IMMEDIATE_eax %94"
"INT_80");
asm("lea_ebx,[esp+DWORD] %8"
"mov_ebx,[ebx]"
"lea_ecx,[esp+DWORD] %4"
"mov_ecx,[ecx]"
"mov_eax, %94"
"int !0x80");
}


int mkdir(char const* a, mode_t b)
{
asm("LOAD_EFFECTIVE_ADDRESS_ebx %8"
"LOAD_INTEGER_ebx"
"LOAD_EFFECTIVE_ADDRESS_ecx %4"
"LOAD_INTEGER_ecx"
"LOAD_IMMEDIATE_eax %39"
"INT_80");
asm("lea_ebx,[esp+DWORD] %8"
"mov_ebx,[ebx]"
"lea_ecx,[esp+DWORD] %4"
"mov_ecx,[ecx]"
"mov_eax, %39"
"int !0x80");
}


int mknod(char const* a, mode_t b, dev_t c)
{
asm("LOAD_EFFECTIVE_ADDRESS_ebx %12"
"LOAD_INTEGER_ebx"
"LOAD_EFFECTIVE_ADDRESS_ecx %8"
"LOAD_INTEGER_ecx"
"LOAD_EFFECTIVE_ADDRESS_edx %4"
"LOAD_INTEGER_edx"
"LOAD_IMMEDIATE_eax %14"
"INT_80");
asm("lea_ebx,[esp+DWORD] %12"
"mov_ebx,[ebx]"
"lea_ecx,[esp+DWORD] %8"
"mov_ecx,[ecx]"
"lea_edx,[esp+DWORD] %4"
"mov_edx,[edx]"
"mov_eax, %14"
"int !0x80");
}


mode_t umask(mode_t m)
{
asm("LOAD_EFFECTIVE_ADDRESS_ebx %4"
"LOAD_INTEGER_ebx"
"LOAD_IMMEDIATE_eax %60"
"INT_80");
asm("lea_ebx,[esp+DWORD] %4"
"mov_ebx,[ebx]"
"mov_eax, %60"
"int !0x80");
}

#endif

0 comments on commit c95bc8d

Please sign in to comment.