Skip to content

Commit

Permalink
replace %d with 666 kinda works
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillerDragon committed Aug 6, 2024
1 parent 59606c3 commit ebca30b
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/macros.asm
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,20 @@
; printdf [format str] [int]
push_registers

; TODO: do not segfault when we write out of those bounds
%define MAX_PRINTF_LEN 2048

; rax = is null terminated format str
str_to_stack %1

%define MAX_PRINTF_LEN 10

sub rsp, MAX_PRINTF_LEN
; r10 is index in final output string
mov r10, 0
; r11 is pointer to start of output string
lea r11, [rbp-MAX_PRINTF_LEN]

mov rcx, 0
.printf_fmt_char_loop:
%%printf_fmt_char_loop:
mov r9b, byte [rax+rcx]
inc rcx

Expand All @@ -71,15 +72,42 @@
mov byte [r11+r10], r9b
inc r10

cmp r9b, '%'
jne %%printf_fmt_char_loop_check_repeat

%%printf_fmt_char_loop_got_percentage:
mov r9b, byte [rax+rcx]
cmp r9b, 'd'
jne %%printf_fmt_char_loop_check_repeat

%%printf_fmt_char_loop_got_fmt_d:

; overwrite the %d
dec r10
inc rcx

mov byte [r11+r10], '6'
inc r10
mov byte [r11+r10], '6'
inc r10
mov byte [r11+r10], '6'
inc r10

%%printf_fmt_char_loop_check_repeat:

cmp r9b, 0
jne .printf_fmt_char_loop
jne %%printf_fmt_char_loop

; print output buffer
printn r11, r10

; frees stack string
; and copy buffer
mov rsp, rbp

; TODO: support \n escape sequence
call print_newline

pop_registers
%endmacro

Expand Down

0 comments on commit ebca30b

Please sign in to comment.