lab2: add int_to_str implementation

This commit is contained in:
ІО-23 Шмуляр Олег 2025-02-09 15:23:45 +02:00
parent 797b9503b7
commit 57c787f87d
1 changed files with 36 additions and 7 deletions

View File

@ -8,15 +8,44 @@
bkpt 0xAB
.endm
.section .data
test_string: .asciz "Hello"
.section .text
lab1_v2:
push {lr}
push.n {lr}
ldr r1, =test_string
BKPT_WRITE r1
mov r0, #2073
bl int_to_str
pop {pc}
pop.n {pc}
int_to_str:
mov r3, sp
@ write newline + NULL terminator
mov r5, #0x000A
strh r5, [r3, #-2]!
@ --- handmade modulo ---
mov r4, #10
iterate:
mov r1, r0 @ save orig to r1
udiv r0, r0, r4 @ orig/10 and overwrite old orig value
mul r2, r0, r4 @ orig/10*10, place separately
subs r5, r1, r2 @ orig - orig/10*10 => modulo (and update flags)
beq check_if_pending_digits_exist
proceed_with_conversion:
add r5, #48 @ int -> char (only digits, so it's fine)
strb r5, [r3, #-1]! @ write next char to RAM
b iterate
check_if_pending_digits_exist:
cmp r0, #0
bne proceed_with_conversion
finish:
BKPT_WRITE r3
mov pc, lr