-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array_user_input.asm
70 lines (60 loc) · 1.75 KB
/
Array_user_input.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
70
.data
line: .asciiz "\n"
size: .asciiz "Enter the size of the array-"
value: .asciiz "\nEnter the value for each element "
.text
main:
li $v0, 4
la $a0, size
syscall
li $v0, 5 #getting size of array from the user
syscall
move $t0, $v0
#Going to create dynamic memory allocation
sll $a0, $v0, 2 # sll performs $a0 = $v0 x 2^2 or in general #$a0 = $v0 x b means $a0 = $v0 x b^2
li $v0, 9 #9 is the system code for service(sbrk) whoes work is to allocate dynamic memory
syscall #v0 store the address of the first byte of the dynamically allocated memory
move $t1, $zero
move $t4, $zero
move $t2, $v0 #to avoid data overwritten, storing value in the $t0
move $t3, $v0
begin:
bge $t1, $t0, while
li $v0, 4
la $a0, value
syscall
li $v0, 5 #user's value input
syscall
sw $v0, 0($t2)
addi $t1, $t1, 1
addi $t2, $t2, 4
j begin
while:
bge $t4, $t0 exit
li $v0, 1
lw $a0, 0($t3) #loading value from the memory
syscall
jal increment
jal newline
j while #jumping unconditionally to while
exit:
li $v0, 10
syscall
increment:
addi $t4, $t4, 1
addi $t3, $t3, 4
jr $ra
newline:
li $v0, 4
la $a0, line
syscall
jr $ra
#----------------Code Explanation-------------#
#All the branch instruction
#b target # unconditional branch to program label target
#beq $t0,$t1,target # branch to target if $t0 = $t1
#blt $t0,$t1,target # branch to target if $t0 < $t1
#ble $t0,$t1,target # branch to target if $t0 <= $t1
#bgt $t0,$t1,target # branch to target if $t0 > $t1
#bge $t0,$t1,target # branch to target if $t0 >= $t1
#bne $t0,$t1,target # branch to target if $t0 != $t1