I'm currently writing a program for a class that requires me to convert a C program to MIPS assembly. The premise is to take two hex values and see how many 1's match up in binary. For instance, here is the C code:
// Count the number of items that match the pattern (have '1' bits in
// the same place as the pattern)
// pu32_a points to the first element of the array
// u32_n is the number of elements in the array
// u32_pat is the pattern to match
uint32_t pmatch(uint32_t* pu32_a, uint32_t u32_n, uint32_t u32_pat) {
uint32_t u32_result;
u32_result = 0;
while(u32_n != 0) {
if((*pu32_a & u32_pat) == u32_pat) u32_result++;
pu32_a++;
u32_n--;
}
return u32_result;
}
uint32_t au32_k[] = {0x80000000, 0xFFFFFFFF, 0x0000A5AA, 0xF0F0F0F0};
uint32_t u32_count;
main() {
u32_count = pmatch(au32_k, 4, 0x0000000F0);
printf("Number of matches is: %d \n", u32_count);
}
And here is my current assembly code:
#
#
# au32_k = {0x80000000, 0xFFFFFFFF,
# 0x0000A5AA, 0xF0F0F0F0}
#
#
.data
au32_k: .space 16
u32_n: .word 4
u32_pat: .word 240
.text
main:
# Store values in $s# registers
addi $s0, $zero, 2147483648
addi $s1, $zero, 4294967295
addi $s2, $zero, 42410
addi $s3, $zero, 4042322160
# Index = $t0
# This loads each of the values into an array
# starting at index $t0.
addi $t0, $zero, 0
sw $s0, au32_k($t0)
addi $t0, $t0, 4
sw $s1, au32_k($t0)
addi $t0, $t0, 4
sw $s2, au32_k($t0)
addi $t0, $t0, 4
sw $s3, au32_k($t0)
# Reset pointer to 0
addi $t0, $t0, -12
# Storing values in argument registers for subroutine
la $a0, au32_k
lw $a1, u32_n
lw $a2, u32_pat
jal pmatch
# This is where I get completely lost.
pmatch:
addi $s0, $zero, 0
addi $s1, $zero, 0
while:
beqz $a1, while_end
if_start:
while_end:
I am completely lost on this part in the C code:
if((*pu32_a & u32_pat) == u32_pat) u32_result++;
I have no clue on how to convert this to assembly, and quite frankly, have no clue how this counts the number of matching 1 bits in two numbers.