Can you make this a MIPS assembly code : *display an image at a given location a
ID: 3667465 • Letter: C
Question
Can you make this a MIPS assembly code:
*display an image at a given location any necessary orientation, and possibly animation
*store any data necessary to achieve this orientation as well as a way to correctly display the image
} Sprite;
where 'data' points to the image data, x and y are the location of the sprite, and h and w are the height and width so that you can print the image correctly and other things (like collision detection).
his is my code so far but it loop is infinity
syscall
main:
load the immediate value 0x0eeeeeee into register $s2
load the immediate value 0 into register $s3
loop:
rotate the contents of $s2 right four bits
save the value in InArr
branch back to loop: if the register $s2 is not equal to $s3
The infinite loop is caused by the fact that $s3 contains a value of 0 and you make no change to this.
What you should have (again written in English) is this
main:
load the immediate value 0x0eeeeeee into register $s2
load the immediate value 7 into register $s3
loop:
rotate the contents of $s2 right four bits
save the value in InArr
subtract 1 from $s3
branch back to loop: if the register $s2 is not equal to $s3
Dry running this gives you the following results
$s2 $s3
1 0x0eeeeeee 7
2 0xe0eeeeee 6
3 0xee0eeeee 5
4 0xeee0eeee 4
5 0xeeee0eee 3
6 0xeeeee0ee 2
7 0xeeeeee0e 1
8 0xeeeeeee0 0
Explanation / Answer
if (i < a)
cptr = record.name;
else
cptr = &(record.name[1]);
To translate this if statement to assembly language, it is easier to first rewrite this code
using goto statements, because assembly languages usually do not provide if-else constructs.
The above code can be rewritten as follows:
if (i >= a)
goto else1;
cptr = record.name;
goto done;
else1: cptr = &(record.name[1]);
done: ...
120 Chapter 3. Assembly-Level Architecture — User Mode
We can implement this modified C code in assembly language with the use of conditional
branch (or conditional jump) instructions, which permit the skipping of one or more
instructions. The exact manner in which a conditional branch checks conditions depends
on the machine. Some machines provide condition codes that are set by arithmetic instructions
and can be tested by conditional branch instructions. Some others like MIPS-I do not
provide condition codes, and instead let conditional branches check the value in a register.
A translation for the above C code to MIPS-I assembly code is given below. In this code,
the if condition evaluation is done using a bge instruction. Notice that in the high-level
language, when an if condition is satisfied, the statement(s) following the if statement is
(are) executed. In the assembly language, by contrast, when a branch condition is satisfied,
the instruction(s) following the branch instruction is (are) skipped. Therefore, we need to
use the complement of the if condition as the branch condition. In this example, the C code
checks for the condition if (i < a); the assembly code checks for the branch condition
bge (branch if greater or equal). Also notice that in the high-level language, once execution
goes to the then portion, the else portion is automatically skipped. However, the assembly
language does not provide such a support, and so an unconditional branch instruction is
used just before the else portion to skip the else portion whenever control goes to the then
portion.
.text
lw $t1, i # Copy the value in memory location i into $t1
lw $t2, a # Copy the value in memory location a into $t2
bge $t1, $t2, else # Branch to label else if $t1 (i) $t2 (a)
la $t1, record # Copy the memory address named record into $t1
sw $t1, cptr # Copy the value in $t1 into memory location named cptr
b done # Branch to label done (to skip the else portion)
else: la $t1, record # Copy the memory address named record into $t1
addu $t1, 1 # Increment $t1 so as to obtain address of record.name[1]
sw $t1, cptr # Copy the value in $t1 into memory location named cptr
done: ...
A series of if-else statements based on a single variable can be expressed as a multiway
branching statement using the C switch statement. Consider the following C code. It
contains a switch statement, with 5 different cases including the default case.
switch (record.length)
{
case 0:
case 1:
*record.name = ’L’;
break;
case 2:
case 3:
3.4. Translating HLL Programs to AL Programs 121
*record.name = ’M’;
break;
default:
*record.name = ’H’;
}
A trivial way of translating this switch statement is to first express it as a series of ifelse
statements, and then translate them as we just did. However, such an approach may
be very inefficient, especially if there are many cases to consider. A more efficient translation
can be performed by incorporating a software structure called jump table. A jump table
is an array that stores the starting addresses of the code segments corresponding to the
different cases of a switch statement. It is indexed by the value of the switch statement’s
control variable.
In the next page we show an assembly language translation of the switch statement
given above. This code begins with the jump table declaration, which is stored in the readonly
data section. The jump table starts at label JT, and contains 4 entries, corresponding
to values 0-3 for record.length. These 4 entries are initialized to 4 labels that are declared
in the .text section.
The .text portion first reads the value of record.length from memory. Then the
default case is handled by checking if the value of this variable is greater than or equal
to 4. If this condition is satisfied, then control is transferred to label default. If the
condition is not satisfied, then we need to index into the jump table. The jump table index
is calculated by scaling the value of record.length by 4, as each table entry occupies 4
bytes. For instance, if record.length has a value of 3, then the mul instruction scales it by
4 to obtain an index of 12. The subsequent lw instruction adds this offset to the jump table
starting address JT, and loads the target address into $t6. The next instruction performs
an unconditional jump to the target address stored in $t6.
.rdata # Store subsequent items in the read only data section
#####################################################################
JT: # Begin jump table here, and initialize it with target addresses
.word case0
.word case1
.word case2
.word case3
#####################################################################
.text # Store subsequent items in the text section
.align 2 # Align next item on a 2
2 byte (32-bit word) boundary
la $t1, record # Load starting address of variable record in $t1
lw $t6, 8($t1) # Copy contents of memory location record.length to $t6
bge $t6, 4, default# Branch to label default if record.length (in $t6) 4
122 Chapter 3. Assembly-Level Architecture — User Mode
mul $t6, $t6, 4 # Scale by 4 to obtain the jump table index
lw $t6, JT($t6) # Copy jump target address from jump table to $t6
j $t6
case0: # case 0
case1: # case 1
li $t15, ’L’
sb $t15, 0($t1) # *record.name = ’L’
b done # break
case2: # case 2
case3: # case 3
li $t15, ’M’
sb $t15, 0($t1) # *record.name = ’M’
b done # break
default: # default
li $t15, ’H’
sb $t15, 0($t1) # *record.name = ’H’
done:
3.4.5 Translating Loops
Loops form a major portion of most programs. T
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.