From 6126cb3f4173ae8e440b35e02e166a5c86378db0 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 19 Nov 2020 19:40:03 +0000 Subject: In progress FossilOrigin-Name: 0e3af2b7fdaa117d45dd21f15813cadbb0a59c81 --- Makefile | 7 +++++++ src/c_bsdsetsid.S | 49 +++++++++++++++++++++++++++++-------------------- src/u_print.S | 28 ++++++++++++++++++++++++++++ src/u_strlen.S | 16 ++++++++++++++++ 4 files changed, 80 insertions(+), 20 deletions(-) create mode 100644 src/u_print.S create mode 100644 src/u_strlen.S diff --git a/Makefile b/Makefile index bbd0931..67a99cc 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,7 @@ PREFIX := /usr/local/ DESTDIR := SRCS := ${SRCS_DIR}c_bsdsetsid.S +SRCS += ${SRCS_DIR}u_print.S SRCS += ${SRCS_DIR}u_strlen.S OBJS = ${SRCS:.S=.o} @@ -75,3 +76,9 @@ clean: ${RM} ${OBJS} ${NAME} .PHONY: all clean + + +# Files prefixes index +# -------------------- +# c_ -> core program related +# u_ -> utils related diff --git a/src/c_bsdsetsid.S b/src/c_bsdsetsid.S index 73ab582..810fe8c 100644 --- a/src/c_bsdsetsid.S +++ b/src/c_bsdsetsid.S @@ -1,28 +1,37 @@ .text -.extern u_strlen -.globl main +.extern u_strlen +.extern u_pout +.extern u_perr +.globl main main: xorq %rcx, %rcx - movq (%rsi, %rcx, 8), %rsi - movq %rsi, %rdi - callq u_strlen - movq $0x1, %rdi - movq %rax, %rdx - xorq %rax, %rax - movb $0x4, %al - syscall - movq $0x1, %rdi - leaq nl, %rsi - movq $0x1, %rdx - xorq %rax, %rax - movq $0x4, %rax - syscall - xorq %rax, %rax - xorq %rax, %rax + callq c_push + movq (%rsi, %rcx, 8), %rdi + callq u_pout + callq c_pop + inc %rcx + callq c_push + movq (%rsi, %rcx, 8), %rdi + callq u_pout + callq c_pop xorq %rdi, %rdi - movb $0x1, %al + xorq %rax, %rax + movb $0x1, %al # SYS_exit syscall +c_push: + movq %rdx, %r8 + movq %rsi, %r9 + movq %rdi, %r10 + retq + +c_pop: + movq %r8, %rdi + movq %r9, %rsi + movq %r10, %rdx + retq + .data - nl: .asciz "\n" +nl: .asciz "\n" +asd: .asciz "hey\n" diff --git a/src/u_print.S b/src/u_print.S new file mode 100644 index 0000000..3866868 --- /dev/null +++ b/src/u_print.S @@ -0,0 +1,28 @@ +.text +.extern u_strlen +.globl u_pout +.globl u_perr + +u_pout: + movq $0x1, %r11 # stdout + jmp u_print + +u_perr: + movq $0x2, %r11 # stderr + jmp u_print + +u_print: + callq u_strlen + movq %rax, %rdx + movq %rdi, %rsi + movq %r11, %rdi + movq $0x4, %rax # SYS_write + syscall + leaq nl, %rsi + movq $0x1, %rdx + movq $0x4, %rax # SYS_write + syscall + retq + +.data +nl: .asciz "\n" diff --git a/src/u_strlen.S b/src/u_strlen.S new file mode 100644 index 0000000..27d9663 --- /dev/null +++ b/src/u_strlen.S @@ -0,0 +1,16 @@ +.text +.globl u_strlen + +u_strlen: + movq %rdi, %rcx + +u_loop: + cmpb $0x0, (%rcx) + je u_ret + inc %rcx + jmp u_loop + +u_ret: + sub %rdi, %rcx + movq %rcx, %rax + retq -- cgit v1.2.3