In: Computer Science
Write a program in MIPS Assembly.
This program will ask the user to enter 20 numbers. The program will store these numbers in an array in memory (sequential memory locations). It will then print out the list of numbers in three different formats:
Also, check for any wrong inputs and handle the error.
Based on the Above Question
int main()
{
int arr[20]; //taking array if size 20 as we require 20
elements
int n,i,temp=0;
int j=0;
// taking input in the array
printf("input 20 numbers one by one");
for(i=0;i<20;i++){
scanf("%d", &arr[i]);
}
//taking input how many numbers does user want in one line
printf("Input the number of digits you want in one line");
scanf("%d", &n);
while(j<20){
//this conditon prints the numbers in array in one line, as much as
user wants
while(temp<n){
if(j!=20){
printf("%d ", arr[j]);
j++;
temp++;}
}
temp=0;
printf("\n");
}
}
When we have tested that our program runs sucessfully in the C
code, then i will suggest convert it into mips, line line by, here
i am using MIPS 5.4 (el) version as you have not specified. If you
want the answer of this assembly language in different version of
MIPS. Please let me know. I will make changes according.
$LC0:
.ascii "input 20 numbers one by one\000"
$LC1:
.ascii "%d\000"
$LC2:
.ascii "Input the number of digits you want in one line\000"
$LC3:
.ascii "%d \000"
main:
addiu $sp,$sp,-128
sw $31,124($sp)
sw $fp,120($sp)
move $fp,$sp
sw $0,28($fp)
sw $0,32($fp)
lui $2,%hi($LC0)
addiu $4,$2,%lo($LC0)
jal printf
nop
sw $0,24($fp)
b $L2
nop
$L3:
addiu $3,$fp,36
lw $2,24($fp)
nop
sll $2,$2,2
addu $2,$3,$2
move $5,$2
lui $2,%hi($LC1)
addiu $4,$2,%lo($LC1)
jal __isoc99_scanf
nop
lw $2,24($fp)
nop
addiu $2,$2,1
sw $2,24($fp)
$L2:
lw $2,24($fp)
nop
slt $2,$2,20
bne $2,$0,$L3
nop
lui $2,%hi($LC2)
addiu $4,$2,%lo($LC2)
jal printf
nop
addiu $2,$fp,116
move $5,$2
lui $2,%hi($LC1)
addiu $4,$2,%lo($LC1)
jal __isoc99_scanf
nop
b $L4
nop
$L6:
lw $3,32($fp)
li $2,20 # 0x14
beq $3,$2,$L5
nop
lw $2,32($fp)
nop
sll $2,$2,2
addiu $3,$fp,24
addu $2,$3,$2
lw $2,12($2)
nop
move $5,$2
lui $2,%hi($LC3)
addiu $4,$2,%lo($LC3)
jal printf
nop
lw $2,32($fp)
nop
addiu $2,$2,1
sw $2,32($fp)
lw $2,28($fp)
nop
addiu $2,$2,1
sw $2,28($fp)
$L5:
lw $2,116($fp)
lw $3,28($fp)
nop
slt $2,$3,$2
bne $2,$0,$L6
nop
sw $0,28($fp)
li $4,10 # 0xa
jal putchar
nop
$L4:
lw $2,32($fp)
nop
slt $2,$2,20
bne $2,$0,$L5
nop
move $2,$0
move $sp,$fp
lw $31,124($sp)
lw $fp,120($sp)
addiu $sp,$sp,128
j $31
nop
I have written
this code with line by line transformation,
If you have any Queries please comment here i will answer them as
soon as possible.
If you like my answer please Upvote / Like it.
Thank you