summaryrefslogtreecommitdiff
path: root/Homework/cs5300/hw-MIPS/code/array.asm
blob: f802ec69768c53ce2742b7606e2d07de1055230c (plain) (blame)
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
.text

.globl	main

main:
	subi $sp, $sp, 4
	sw $ra, 0($sp) # Put return address on the bottom of the stack
	
	subi $sp, $sp, 12 # 3 elements on the stack
	la $t0, 0($sp) # Part of the dope vector - address of the array
	li $t1, 3 # Might as well put the array length here too
	li $s0, 1 # Put in registers...
	li $s1, 3
	li $s2, 5
	sw $s0, 0($sp) # Push on stack...
	sw $s1, 4($sp)
	sw $s2, 8($sp)
	
	subi $sp, $sp, 8 # Dope vector two words: <address> <length>
	sw $t0, 0($sp)
	sw $t1, 4($sp)
	
	jal fee
	
	lw $a0, 0($sp)
	addi $sp, $sp, 4
	
	li $v0, 1
	syscall
	
	li $v0, 10
	syscall

fee: 
	li $t0, 0 # i
	li $t1, 0 # sum
	lw $t2, 4($sp) # n
	lw $t3, 0($sp) # address
	addi $sp, $sp, 16
	
	b loop

loop:
	li $t4, 4 # sizeof(int)
	
	mult $t0, $t4
	mflo $t6
	add $t5, $t3, $t6
	lw $t7, ($t5) # element = address + i * 4
	
	add $t1, $t1, $t7 # sum = sum + element
	addi $t0, $t0, 1 # i = i + 1
	
	bne $t0, $t2, loop
	
	# return sum
	subi $sp, $sp, 4
	sw $t1, 0($sp)
	
	jr $ra

# Start .data segment (data!)
.data