Code:
#include <stdio.h>
#include <string.h>
struct label
{
char opcode[99];
char rs[99];
char rt[99];
char rd[99];
};
struct label labels[200];
char line[99];
void reduceOpcode(int linenumber)
{
char temp[99];
int i,j;
i=j=0;
while(line[i] == labels[linenumber].opcode[i])
{
i++;
}
while(line[i] == 9 || line[i] == 32 || line[i] == 36 || line[i] == 44)
{
i++;
}
while(line[i] != 0 && line[i] != 10)
{
temp[j] = line[i];
j++;
i++;
}
temp[j] = 0;
strcpy(line, temp);
}
void insertintolabel(int linenumber)
{
char temp[99];
strcpy(temp, line);
strcpy(labels[linenumber].opcode, strtok(line, " "));
strcpy(line, temp);
reduceOpcode(linenumber);
if (line != 0)
{
strcpy(labels[linenumber].rs, strtok(line, " $, "));// THIS IS WHERE IT GOES WRONG !!
}
labels[linenumber].opcode, labels[linenumber].rs, labels[linenumber].rt, labels[linenumber].rd);
}
void initiate_labels(FILE* f)
{
char temp[99];
int linenumber, i, j;
linenumber = i = j = 0;
while(!feof(f)) //this while loop removes all lines before the .data line in the mips code
{
fgets(line,99,f);
i = firstchar(line);
if(line[i] == 46)
{
while(line[i] != '#' && line[i] != 0 && line[i] != 10)//saves the necessary part of the line into temp
{
temp[j]=line[i];
i++;
j++;
}
temp[j] = 0;
j=0;
strcpy(line, temp);
linenumber = 1;
insertintolabel(linenumber);//sends the dataline to insertintolabel
break;
}
}
while(!feof(f))//sends the rest of the file-lines on to insertintolabel, with all leading spaces and tabs removed
{
fgets(line, 99, f);
i = firstchar(line);
if(i != -1 && line[i] != 35)//fjerner alle tomme linier
{
linenumber++;
j = 0;
while(line[i] != '#' && line[i] != 0 && line[i] != 10)
{
temp[j] = line[i];
i++;
j++;
}
temp[j] = 0;
strcpy(line, temp);
j=0;
insertintolabel(linenumber);
}
}
}
int firstchar(char* string)//finds the first index of a string that is not a tab or a space
{
int i = 0;
while(string[i] == 32 || string[i] == 9)
{
i++;
}
if (string[i] == 10)
{
return -1;
}
return i;
}
main()//trying to save a file of mips code into an array, labels, of label structures that contain the opcode, rs, rt, and rd as char*.
{
FILE* myfile;
myfile = fopen("C:\\g24\\test1.asm", "r");
initiate_labels(myfile);
fclose(myfile);
}
myfile:
Code:
# This code does not use any arguments you may call it with. It reads 10 integers from console,
# performs the calculations and outputs the result containing the same integers.
# This code uses the instructions found in the MIPS-lite set, together with some pseudo-instructions
# and 'syscall'.
.data
.align 2
array: .space 40
space: .asciiz " "
.text
main:
add $a1, $zero, 10
la $a0, array
jal readInts
la $a0, array
add $a1, $a0, 36
jal partition
add $a1, $zero, 10
la $a0, array
jal printInts
add $v0, $zero, 10
syscall
swap:
lw $t0, 0($a0)
lw $t1, 0($a1)
sw $t1, 0($a0)
sw $t0, 0($a1)
jr $ra
partition:
sub $sp, $sp, 24
sw $ra, 20($sp)
sw $s5, 16($sp)
sw $s4, 12($sp)
sw $s3, 8($sp)
sw $s1, 4($sp)
sw $s0, 0($sp)
move $s0, $a0
move $s1, $a1
lw $s3, 0($s1)
move $s4, $s0
sub $s4, $s4, 4
move $s5, $s0
loop:
beq $s5, $s1, exit
lw $t0, 0($s5)
slt $t1, $t0, $s3
beq $t1, $zero, over
add $s4, $s4, 4
move $a0, $s4
move $a1, $s5
jal swap
over:
add $s5, $s5, 4
j loop
exit:
add $a0, $s4, 4
move $a1, $s1
jal swap
add $v0, $s4, 4
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $s3, 8($sp)
lw $s4, 12($sp)
lw $s5, 16($sp)
lw $ra, 20($sp)
add $sp, $sp, 24
jr $ra
readInts:
add $v0, $zero, 5
beq $a1, $zero, endRead
syscall
sub $a1, $a1, 1
sw $v0, ($a0)
add $a0, $a0, 4
j readInts
endRead:
jr $ra
printInts:
move $t0, $a0
print:
add $v0, $zero, 1
beq $a1, $zero, endPrint
lw $a0, ($t0)
syscall
sub $a1, $a1, 1
add $t0, $t0, 4
la $a0, space
add $v0, $zero, 4
syscall
j print
endPrint:
jr $ra
Any help is greatly appreciated!