-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountDigitFunction.asm
69 lines (53 loc) · 971 Bytes
/
CountDigitFunction.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#A program to count digits of a number.
.data
scan: .asciiz "Enter the number: "
answer: .asciiz "Number of digits = "
newLine: .asciiz "\n"
.globl main
.text
countDigits:
addi $sp, $sp, -4
sw $s1, 0($sp)
blt $s1, $zero, Abs #to handle the case of negative numbers
beq $s1, $zero, caseZero #to handle case of entering just zero
caseZero:
addi $t2, $t2, 1
j Exit
Abs:
li $t5 , -1
mul $s1, $s1, $t5
j loop
loop:
ble $s1 , $zero , Exit
div $s1 , $s1 , $t0
addi $t2 , $t2 , 1
j loop
Exit:
la $a0 , answer
li $v0 , 4
syscall
add $a0 , $t2 , $zero
li $v0, 1
syscall
lw $s1, 0($sp)
addi $sp, $sp, 4
jr $ra
main:
li $s1 , 0
la $a0 , scan
li $v0 , 4
syscall
la $a0 , newLine
li $v0 , 4
syscall
li $v0 , 5
syscall
add $s1 , $v0 , $zero
li $t0 , 10
li $t2 , 0
li $t3 , 1
#Arch of MIPS make this code inefficent as I didn't send the parameters in $a registers
jal countDigits
li $v0 , 10
syscall
.end main