Reputation: 110
So, I recently made a code to count the number of binary 1's in C-code and in MIPS code. I did so in C by using a remainder value and increment a count_one variable. In MIPS, I did the same program but I shifted the bytes of the number until it counted all of the 1's. Howver, I want to try to learn how to use pointers but I can not seem to grasp the concept. My MIPS code is as follows:
.data
prompt: .asciiz "Enter a integer: "
.text
li $v0,4
la $a0, prompt
syscall
li $v0,5
syscall
move $s0,$v0
j count
count:
beq $s0,0, exit
andi $t0,$s0,1
add $t1,$t1,$t0
srl $s0,$s0,1
j count
exit:
move $a0,$t1
la $v0,1
syscall
li $v0,10
syscall
I get this complete MIPS code but I am unsure on how pointers completely work in MIPS and after reading I still don't understand. Any advice on how to implement pointers?
Upvotes: 1
Views: 2546
Reputation: 846
Here is an example code that translate the flowing C code in MIPS.
In order to save and restore the preserved registers it make some place on the stack then using sw
and lw
to save and restore those registers.
int leaf_example(int g, int h, int i, int j) {
int f;
f = (g + h) - (i + j);
return f;
}
.text
main:
addi $a0,$0,1 #argument 0 = 1
addi $a1,$0,2 #argument 1 = 2
addi $a2,$0,3 #argument 2 = 3
addi $a3,$0,4 #argument 3 = 4
jal leaf # call function leaf
add $s0,$v0,$zero # return value
li $v0,10
syscall
leaf:
addi $sp, $sp, -12 #adjust stack to make room for 3 items
sw $s0, 8($sp) #save register $s0 for use in memory location 8
sw $t0, 4($sp) #save register $t0 for use in memory location 4
sw $t1, 0($sp) #save register $t1 for use in memory location 0
add $t0, $a0, $a1 #register $t0 contains $a0 + $a1
add $t1, $a2, $a3 #register $t1 contains $a2 + $a3
sub $s0, $t0, $t1 #$t0 = $t0 - $t1 -> $t0 = ($a0 + $a1) - ($a2 + $a3)
add $v0, $s0, $zero #copy $s0 to return register $v0
#Before returning, we restore three original values
#of registers we pushed onto stack by popping them
lw $t1, 0($sp) #restore register $t1 for caller
lw $t0, 4($sp) #restore register $t0 for caller
lw $s0, 8($sp) #restore register $s0 for caller
addi $sp, $sp, 12 #adjust stack to delete 3 items
jr $ra #jump back to calling routine
Upvotes: 1
Reputation: 8220
Most common the command determines the mind of data.
For example in pseudo code
inc $a0
this command increment data in register $a0
work with it as with number
lw $s1, 0($a0)
this command load data from memory pointed by register $a0
work with it as with pointer
Upvotes: 1