Skip to content

Instantly share code, notes, and snippets.

@JustLinuxUser
Last active February 15, 2025 16:43
Show Gist options
  • Save JustLinuxUser/f51c28f9925e81ffc2a3a1f875409a73 to your computer and use it in GitHub Desktop.
Save JustLinuxUser/f51c28f9925e81ffc2a3a1f875409a73 to your computer and use it in GitHub Desktop.
global _start
msg: db "global _start",10,"msg: db |",10,"newline_seq: db 34, 44, 49, 48, 44, 34, 0",10,"end_seq: db 34, 44, 48, 0",10,"ft_putchar_real: ; recieves a str ptr, in rsi, only prints the first char",10," mov rax, 1 ; write syscall",10," mov rdi, 1 ; fd 1",10," mov rdx, 1 ; 1 byte",10," syscall ",10," ret",10,"_start:",10," mov rsi, msg",10," call putstr ; Call the main function, then exit",10," mov rax, 60 ; exit syscall",10," mov rdi, 0 ; status 0",10," syscall",10,"putstr:; gets input from rsi",10," cmp byte [rsi], 124 ; if is a pipe symbol",10," jne skip ; start of print_escaped",10," push rsi ; save rsi",10," mov rsi, end_seq ; use the first char of that string",10," call ft_putchar_real; print one semi colon",10," mov rsi, msg ; start printing the formatted str",10," loop2:",10," cmp byte [rsi], 10",10," jne skip_format_newline ; if newline",10," push rsi",10," mov rsi, newline_seq",10," call putstr",10," pop rsi",10," jmp skip_print2",10," skip_format_newline: ; else",10," call ft_putchar_real ; print normal char",10," skip_print2:",10," inc rsi ; str++",10," cmp byte [rsi], 0 ",10," jne loop2 ; if *str == 0",10," mov rsi, end_seq ; we don't care about this rsi anymore",10," call putstr",10," pop rsi",10," jmp skip_print ; end of print_escaped",10,"skip: ; print normal char",10," call ft_putchar_real ; ft_putchar (str)",10,"skip_print:",10," inc rsi ; str++",10," cmp byte [rsi], 0 ",10," jne putstr ; if *str == 0",10," ret",10,"",0
newline_seq: db 34, 44, 49, 48, 44, 34, 0
end_seq: db 34, 44, 48, 0
ft_putchar_real: ; recieves a str ptr, in rsi, only prints the first char
mov rax, 1 ; write syscall
mov rdi, 1 ; fd 1
mov rdx, 1 ; 1 byte
syscall
ret
_start:
mov rsi, msg
call putstr ; Call the main function, then exit
mov rax, 60 ; exit syscall
mov rdi, 0 ; status 0
syscall
putstr:; gets input from rsi
cmp byte [rsi], 124 ; if is a pipe symbol
jne skip ; start of print_escaped
push rsi ; save rsi
mov rsi, end_seq ; use the first char of that string
call ft_putchar_real; print one semi colon
mov rsi, msg ; start printing the formatted str
loop2:
cmp byte [rsi], 10
jne skip_format_newline ; if newline
push rsi
mov rsi, newline_seq
call putstr
pop rsi
jmp skip_print2
skip_format_newline: ; else
call ft_putchar_real ; print normal char
skip_print2:
inc rsi ; str++
cmp byte [rsi], 0
jne loop2 ; if *str == 0
mov rsi, end_seq ; we don't care about this rsi anymore
call putstr
pop rsi
jmp skip_print ; end of print_escaped
skip: ; print normal char
call ft_putchar_real ; ft_putchar (str)
skip_print:
inc rsi ; str++
cmp byte [rsi], 0
jne putstr ; if *str == 0
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment