-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample3.s
executable file
·83 lines (56 loc) · 2.19 KB
/
Example3.s
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
71
72
73
74
75
76
77
78
79
80
81
82
83
.data 0x10000000
alp: .byte ' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
msg1: .asciiz "Enter length of array: "
msg2: .asciiz "Enter an array element "
msg2_2: .asciiz ": "
.text
.globl main
main: ##################### get length of array #########################
li $v0, 4 # print enter length of arr
la $a0, msg1
syscall
li $v0, 5 # reads integer input
syscall
addu $t1, $v0, $0 # move integer to t1
##################### space allocation ############################
sub $t2, $0, $t1 # allocates some space in stack
add $sp, $sp, $t2
add $t0, $0, $sp
##################### loop section ################################
addu $t2, $0, $0 # initializes loop index to 0
loop: slt $t3, $t2, $t1 # compares index with array length
beq $t3, $0, loop2_init
li $v0, 4 # prints enter array element string
la $a0, msg2
syscall
li $v0, 1
add $a0, $0, $t2
syscall
li $v0, 4
la $a0, msg2_2
syscall
li $v0, 5 # reads integer input
syscall
add $t4, $t0, $t2 # finds the address to put input into
addi $t2, $t2, 1 # increases index by 1
sb $v0, 0($t4) # puts integer input into stack
j loop
##################### finds corresponding letter ##################
loop2_init: addu $t2, $0, $0 # initializes index to 0
loop2: slt $t3, $t2, $t1 # compares index with array length
beq $t3, $0, exit
add $t4, $t0, $t2 # gets the ith integer
lb $t3, 0($t4) #
addi $t2, $t2, 1 # increases index by 1
addi $t7, $0, -1 # puts -1 into t7
slt $t5, $t7, $t3 # compares ith integer with -1
slti $t6, $t3, 28 # compares ith integer with 28
and $t5, $t5, $t6 # ands them to be sure that integer are in range
beq $t5, $0, loop2 # if not in range goes to loop2
la $t6, alp # loads the corresponding letter from alp
add $t6, $t3, $t6
lb $a0, 0($t6)
li $v0, 11 # prints the corresponding letter
syscall
j loop2
exit: jr $ra