Reputation: 1
I have a program to find maximum prime number in a given array coded in C: https://pastebin.com/1HrerxsH
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, N = 1000, l = 0;
int a[N], primeArr[l];
printf("Array of numbers: \n");
for (i = 0; i < N; i++)
{
a[i] = rand();
printf("%d ", a[i]);
}
printf("\n\n");
printf("Array of prime numbers: \n");
for (i = 0; i < N; i++)
{
int k, numDivisor = 0;
for (k = 1; k <= a[i]; k++)
if (a[i] % k == 0)
numDivisor++;
if (numDivisor == 2)
{
primeArr[l] = a[i];
printf("%d ", primeArr[l]);
l++;
}
}
printf("\n\n");
if(l > 0)
{
printf("Max prime numbers: \n");
int max = primeArr[0];
for (i = 1; i < l; i++)
{
if (primeArr[i] >= max)
max = primeArr[i];
}
printf("%d", max);
return 0;
}
else
return -1;
}
After compile it into MIPS assembly by MIPS CROSS COMPILER, I obtain the code in .s file extension: https://pastebin.com/NTMHY9Jw
.file 1 "test3.c"
.rdata
.align 2
$LC0:
.ascii "Array of numbers: \n\000"
.align 2
$LC1:
.ascii "%d \000"
.align 2
$LC2:
.ascii "\n\n\000"
.align 2
$LC3:
.ascii "Array of prime numbers: \n\000"
.align 2
$LC4:
.ascii "Max prime numbers: \n\000"
.align 2
$LC5:
.ascii "%d\000"
.text
.align 2
.globl main
.ent main
main:
.frame $fp,72,$31 # vars= 40, regs= 3/0, args= 16, gp= 0
.mask 0xc0010000,-8
.fmask 0x00000000,0
addiu $sp,$sp,-72
sw $31,64($sp)
sw $fp,60($sp)
sw $16,56($sp)
move $fp,$sp
sw $sp,40($fp)
li $2,1000 # 0x3e8
sw $2,20($fp)
sw $0,24($fp)
lw $2,20($fp)
sll $2,$2,2
addiu $2,$2,7
addiu $2,$2,7
srl $2,$2,3
sll $2,$2,3
subu $sp,$sp,$2
addiu $2,$sp,16
addiu $2,$2,7
srl $2,$2,3
sll $2,$2,3
sw $2,44($fp)
lw $2,24($fp)
sll $2,$2,2
addiu $2,$2,7
addiu $2,$2,7
srl $2,$2,3
sll $2,$2,3
subu $sp,$sp,$2
addiu $2,$sp,16
addiu $2,$2,7
srl $2,$2,3
sll $2,$2,3
sw $2,48($fp)
la $4,$LC0
jal printf
sw $0,16($fp)
$L2:
lw $2,16($fp)
lw $3,20($fp)
slt $2,$2,$3
beq $2,$0,$L3
lw $2,16($fp)
sll $2,$2,2
lw $3,44($fp)
addu $16,$2,$3
jal rand
sw $2,0($16)
lw $2,16($fp)
sll $2,$2,2
lw $4,44($fp)
addu $2,$2,$4
la $4,$LC1
lw $5,0($2)
jal printf
lw $2,16($fp)
addiu $2,$2,1
sw $2,16($fp)
j $L2
$L3:
la $4,$LC2
jal printf
la $4,$LC3
jal printf
sw $0,16($fp)
$L5:
lw $2,16($fp)
lw $3,20($fp)
slt $2,$2,$3
beq $2,$0,$L6
sw $0,32($fp)
li $2,1 # 0x1
sw $2,28($fp)
$L8:
lw $2,16($fp)
sll $2,$2,2
lw $3,44($fp)
addu $2,$2,$3
lw $3,0($2)
lw $2,28($fp)
slt $2,$3,$2
bne $2,$0,$L9
lw $2,16($fp)
sll $2,$2,2
lw $4,44($fp)
addu $2,$2,$4
lw $3,0($2)
lw $2,28($fp)
div $0,$3,$2
bne $2,$0,1f
break 7
1:
mfhi $2
bne $2,$0,$L10
lw $2,32($fp)
addiu $2,$2,1
sw $2,32($fp)
$L10:
lw $2,28($fp)
addiu $2,$2,1
sw $2,28($fp)
j $L8
$L9:
lw $3,32($fp)
li $2,2 # 0x2
bne $3,$2,$L7
lw $2,24($fp)
sll $2,$2,2
lw $4,48($fp)
addu $3,$2,$4
lw $2,16($fp)
sll $2,$2,2
lw $4,44($fp)
addu $2,$2,$4
lw $2,0($2)
sw $2,0($3)
lw $2,24($fp)
sll $2,$2,2
lw $3,48($fp)
addu $2,$2,$3
la $4,$LC1
lw $5,0($2)
jal printf
lw $2,24($fp)
addiu $2,$2,1
sw $2,24($fp)
$L7:
lw $2,16($fp)
addiu $2,$2,1
sw $2,16($fp)
j $L5
$L6:
la $4,$LC2
jal printf
lw $2,24($fp)
blez $2,$L13
la $4,$LC4
jal printf
lw $4,48($fp)
lw $2,0($4)
sw $2,32($fp)
li $2,1 # 0x1
sw $2,16($fp)
$L14:
lw $2,16($fp)
lw $3,24($fp)
slt $2,$2,$3
beq $2,$0,$L15
lw $2,16($fp)
sll $2,$2,2
lw $3,48($fp)
addu $2,$2,$3
lw $3,0($2)
lw $2,32($fp)
slt $2,$3,$2
bne $2,$0,$L16
lw $2,16($fp)
sll $2,$2,2
lw $4,48($fp)
addu $2,$2,$4
lw $2,0($2)
sw $2,32($fp)
$L16:
lw $2,16($fp)
addiu $2,$2,1
sw $2,16($fp)
j $L14
$L15:
la $4,$LC5
lw $5,32($fp)
jal printf
lw $sp,40($fp)
sw $0,36($fp)
j $L1
$L13:
lw $sp,40($fp)
li $2,-1 # 0xffffffffffffffff
sw $2,36($fp)
$L1:
lw $2,36($fp)
move $sp,$fp
lw $31,64($sp)
lw $fp,60($sp)
lw $16,56($sp)
addiu $sp,$sp,72
j $31
.end main
But when I run the .s file in MARS4_5, it appears these errors: ".align" directive cannot appear in text segment
".ascii" directive cannot appear in text segment
"1" is not a recognized operator
"1f": operand is of incorrect type
"$31": operand is of incorrect type
How do I fix it?
Upvotes: 0
Views: 1149
Reputation: 2320
MARS doesn't recognize the rdata segment; change it to just data. For good measure, remove all the .align directives, since they were inserted the compiler to improve performance but aren't really necessary if you're just running this in MARS. Finally, to get rid of the "operand is of incorrect type", you'll need to figure out where the computer actually intended for those labels to point and patch it up manually.
Upvotes: 1