From 746dc4f2a61bbf14937118580e915690136adb8c Mon Sep 17 00:00:00 2001 From: Keerthana S Date: Fri, 8 May 2026 16:39:14 +0530 Subject: [PATCH] fix q7 disarium Refactor assembly code for readability and correctness. --- 2025101034/q7/q7.s | 89 ++++++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 34 deletions(-) diff --git a/2025101034/q7/q7.s b/2025101034/q7/q7.s index 510f33f..8f545a1 100644 --- a/2025101034/q7/q7.s +++ b/2025101034/q7/q7.s @@ -1,46 +1,67 @@ .data -fmt_scan: .string "%lld" -fmt_print: .string "lld\n" -fmt_true: .string "TRUE\n" +fmt_scan: .string "%lld" +fmt_true: .string "TRUE\n" fmt_false: .string "FALSE\n" n: .dword 0 .global main main: -addi sp, sp, -32 -sd ra, 24(sp) -sd s0, 16(sp) -sd s1, 8(sp) + addi sp, sp, -32 + sd ra, 24(sp) + sd s0, 16(sp) + sd s1, 8(sp) -la a0, fmt_scan -la a1, n -call scanf -ld s0, n #s0=n + la a0, fmt_scan + la a1, n + call scanf -li t0, 10 #t0=10 -li t1, 0 #cnt=0 -mv t2, s0#t2=s0 + ld s0, n + li t0, 10 + li t1, 0 + mv t2, s0 digit_cnt: -ble t2, zero, done #n<=0 -div t2, t2, t0 #n=n/10 -addi t1, t1, 1 #cnt++ -j digit_cnt + ble t2, zero, count_done + div t2, t2, t0 + addi t1, t1, 1 + j digit_cnt + +count_done: + li s1, 0 + mv t2, s0 loop: -ble s0, zero, done # n<=0 -rem t3, t2, t0 #t3 = last digit -li t2, 0 #sum=t2=0 - -li t4, 1 -inner_loop: -ble t1, zer0, inner_done #cnt-- -mul t2, t3, t3 - - -sd ra, 24(sp) -sd s0, 16(sp) -sd s1, 8(sp) -addi sp, sp, 32 -li a0 0 -ret \ No newline at end of file + ble t2, zero, check + rem t3, t2, t0 + div t2, t2, t0 + li t4, 1 + mv t5, t1 + +power_loop: + ble t5, zero, power_done + mul t4, t4, t3 + addi t5, t5, -1 + j power_loop + +power_done: + add s1, s1, t4 + addi t1, t1, -1 + j loop + +check: + bne s1, s0, print_false + la a0, fmt_true + call printf + j end + +print_false: + la a0, fmt_false + call printf + +end: + ld ra, 24(sp) + ld s0, 16(sp) + ld s1, 8(sp) + addi sp, sp, 32 + li a0, 0 + ret