-
Notifications
You must be signed in to change notification settings - Fork 0
/
35.SumOfNFn.asm
69 lines (51 loc) · 984 Bytes
/
35.SumOfNFn.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
.text
.globl main
main:
# ip
li $v0, 4
la $a0, ip
syscall
# get ip
li $v0, 5
syscall
# move to $a0
move $a0, $v0
# call sum
jal sum
# back to main after function
# load result from v0 to s0
move $s0, $v0
# op
li $v0, 4
la $a0, op
syscall
# ans = $s0
li $v0, 1
move $a0, $s0
syscall
# exit
li $v0, 10
syscall
sum:
# $t0 move from n to 0
move $t0, $a0
# $t1 sum variable
li $t1, 0
loop1:
# check if $t0 is 0
beq $t0, 0, exit_label
# else_case
# add n = t0 to t1- sum
add $t1, $t1, $t0
# dec $t0 = n by 1
addi $t0, $t0, -1
# jump to loop1
j loop1
exit_label:
# afer loop move result in t1 to vo-> return reg
move $v0, $t1
# return to main and continue from there
jal $ra
.data
ip: .asciiz "Enter n: "
op: .asciiz "Sum of N : "