-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathfirst1pos.s
89 lines (85 loc) · 1.37 KB
/
first1pos.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
84
85
86
87
88
89
main:
lui $a0,0x8000 # should be 31
jal first1pos
jal printv0
lui $a0,0x0001 # should be 16
jal first1pos
jal printv0
li $a0,1 # should be 0
jal first1pos
jal printv0
add $a0,$0,$0
jal first1pos
jal printv0
li $v0,10
syscall
first1pos: # placeholder to call different versions: first1posshift of first1posmask
addiu $sp, $sp, -4
sw $ra, 0($sp)
jal first1posshift
# jal first1posmask
lw $ra, 0($sp)
addiu $sp, $sp, 4
jr $ra
##################
first1posshift:
addi $sp,$sp,-4
sw $s0,0($sp)
addi $s0,$zero,31
LOOPfirst:
beq $a0,$zero,returnMinus1
slt $t0,$a0,$zero
bne $t0,$zero,finishFirst
addi $s0,$s0,-1
sll $a0,$a0,1
j LOOPfirst
returnMinus1:
li $v0,-1
lw $s0,0($sp)
addi $sp,$sp,4
jr $ra
finishFirst:
move $v0,$s0
lw $s0,0($sp)
addi $sp,$sp,4
jr $ra
#################
first1posmask:
addi $sp,$sp,-8
sw $s0,0($sp)
sw $s1,4($sp)
addi $s0,$zero,31
li $s1,0x80000000
LOOPSecond:
li $t0,0
beq $a0,$zero,returnMinus1Again
and $t0,$a0,$s1
bne $t0,$zero,finishSecond
addi $s0,$s0,-1
srl $s1,$s1,1
j LOOPSecond
returnMinus1Again:
li $v0,-1
lw $s0,0($sp)
lw $s1,4($sp)
addi $sp,$sp,8
jr $ra
finishSecond:
move $v0,$s0
lw $s0,0($sp)
lw $s1,4($sp)
addi $sp,$sp,8
jr $ra
#################
printv0:
addi $sp,$sp,-4
sw $ra,0($sp)
add $a0,$v0,$0
li $v0,1
syscall
li $v0,11
li $a0,'\n'
syscall
lw $ra,0($sp)
addi $sp,$sp,4
jr $ra