write a MIPS assimbly language program that disblay the contents of regiester $t
ID: 3690904 • Letter: W
Question
write a MIPS assimbly language program that disblay the contents of regiester $t0 as binary string????????
EXAMBLE llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll
2. BITWISE LOGICAL INSTRUCTIONS
Instructions
Description
and
rd, rs, rt
rd = rs & rt
andi
rt, rs, immediate
rt = rs & immediate
or
rd, rs, rt
rd = rs | rt
ori
rt, rs, immediate
rd = rs | immediate
nor
rd, rs, rt
rd = ! ( rs | rt )
xor
rd, rs, rt
To do a bitwise logical Exclusive OR.
xori
rt, rs, immediate
The main usage of bitwise logical instructions are: to set, to clear, to invert, and to isolate some selected bits in the destination operand. To do this, a source bit pattern known as a mask is constructed. The individual mask bits are chosen based on the following properties of AND, OR, and XOR with Z representing a bit (either 0 or 1):
AND
OR
XOR
Z AND 0 = 0
Z OR 0 = Z
Z XOR 0 = Z
Z AND 1 = Z
Z OR 1 = 1
Z XOR 1 = ~Z
The AND instruction can be used to CLEAR specific destination bits while preserving the others. A zero mask bit clears the corresponding destination bit; a one mask bit preserves the corresponding destination bit.
The OR instruction can be used to SET specific destination bits while preserving the others. A one mask bit sets the corresponding destination bit; a zero mask bit preserves the corresponding destination bit.
The XOR instruction can be used to INVERT specific destination bits while preserving the others. A one mask bit inverts the corresponding destination bit; a zero mask bit preserves the corresponding destination bit.
Example 1:
The following code fragment will clear bit 2, 4, 6, and 7 of the register $t2:
addi $t0, $zero, 0xFF2B
andi $t2, $t2, $t0
Example 2:
The following code fragment will set bit 7, 6, 5, 3 and 0 of the register $t2 using OR operation:
ori $t2, $t2, 0x00E9
Example 3:
The following code fragment will toggle bit 7, 2, and 0 of the register $t2 using XOR operation:
xori $t2, $t2, 0x85
3. SHIFT INSTRUCTIONS
Instruction
Description
sll
rd, rs, sa
rd = rs << sa (Shift Left Logical)
sllv
rd, rt, rs
rd = rt << rs (To left-shift a word by a variable number of bits)
sra
rd, rs, sa
The contents of the low-order 32-bit word of rs are shifted right, duplicating the sign-bit (bit 31) in the emptied bits; the word result is placed in rd. The bit-shift amount is specified by sa.
srav
rd, rt, rs
rd = rt >> rs (Arithmetic)
srl
rd, rs, sa
rd = rs >> sa (Shift Right Logical)
srlv
rd, rt, rs
rd = rt >> rs
Logical Shift instructions are useful mainly in these situations:
To manipulate bits;
To multiply and divide unsigned numbers by a power of 2.
Example 4:
The following code fragment will multiply the content of register $t0 with 80:
sll $t1, $t0, 4 # *16
sll $t0, $t0, 6 # *64
addu $t0, $t0, $t1
Example 5:
Explain what the following code fragment will do?
addu $t1,$t0, $zero
sll $t0,$t0,4
srl $t1,$t1,4
or $t1,$t1,$t0
4. CONDITIONAL AND UNCONDITIONAL BRANCH INSTRUCTIONS
Instruction
Description
bgez
rs, L
if ( rs >= 0 ) go to L;
bgtz
rs, L
if ( rs > 0 ) go to L;
blez
rs, L
if ( rs <= 0 ) go to L;
bltz
rs, L
if ( rs < 0 ) go to L;
bne
rs, rt, L
if (rs != rt) go to L;
beq
rs, rt, L
if (rs == rt) go to L;
slt
rd, rs, rt
if ( rs < rt ) rd=1; else rd=0; rs and rt are signed integers.
sltu
rd, rs, rt
Same as slt except rs and rt are unsigned integers.
slti
rt, rs, immediate
if ( rs < signed immediate ) rd=1; else rd=0;
sltiu
rt, rs, immediate
if ( rs < unsigned immediate ) rd=1; else rd=0;
J
L
go to L
Example 6:
Following is a MIPS assembly language program that calculates the sum of all positive integers less than or equal to N and displays the result on the monitor. Assuming that N is stored in the register $t0.
Algorithm
Assembly Language
loop:
$t0 ¬ N;
loop:
li
$t0, N
$t1 ¬ 1;
li
$t1, 1
$a0 ¬ 0;
add
$a0, $zero, $zero
if ($t1 > $t0) go to print;
sltu
$t2, $t0, $t1
$a0 ¬ $a0 + $t1;
bgtz
$t2, print
$t1 ¬ $t1 + 1;
addu
$a0, $a0, $t1
go to loop;
addi
$t1, $t1, 1
print:
display $a0;
j
loop
exit;
print:
Exercise 3.1
1.aMIPSlanguageprogramconvertsalllowercaselettersofastringtouppercaseones.
2.
Algorithm
Assembly Language
$t0 ¬ N – 1;
$t1 ¬ 1;
$a0 ¬ 1; display $a0;
loop: display $a0;
$t0 ¬ $t0 – 1;
if ($t0 == 0) stop;
$a0 ¬ $a0 + $t1;
$t1 ¬ $a0 – $t1; go to loop;
stop:
3.acompleteprogramtodisplayalltheODDpositivelessthan1000.
5. ARRAYS
To declare an array you need: An array label, the number of elements, the size of each element, and optionally, the initial value of each element.
Example 7:
.data
A01: .byte 'a', 'k', 'p', 5 # A01 is an array of 4 bytes: {'a', 'k', 'p', 5}
A02: .word 5, 6, -9, 7 # A02 is an array of 4 words: {5, 6, -9, 7}
B02: .space 40 # allocate 40 consecutive bytes, with storage uninitialized
# could be used as a 40-element character array
# 10-element integer array;
# a comment should indicate which!
var1: .half 3 # create a single short integer variable with initial value 3
B03: .word -1:30 # allocate 30 consecutive words with each element
# initialized with -1.
5.1 Traversing Single-Dimensional Array
To access elements of an array, we have to know the address of that element. Because all elements have the same size, the address of an element of the array can be formulated as:
where,
The first element of the array is (i)ndexed 0, and The size-of-element is the number of bytes in a single array element. The size-of-element either is one byte, 2 bytes, 4 bytes, or 8 bytes.
Example 8:
The following code fragment is to access the sixth element of table1 array, and demonstrates two ways of accessing that element:
.data
table1: .word 4, 5, 6, 7, 8, 9, 10, 21
.text
la $t0, table1
lw $t1, 20($t0)
addiu $t2, $t0, 20
lw $t1, 0($t2)
5.2 Two-Dimensional Arrays
Two or higher dimensional arrays are treated as the same as simple single-dimensional arrays. To declare the array M[rows][cols] of byte-sized elements: Calculate the number of elements in the array: number-of-elements = rows * cols. For example,
M: .byte 0:number_of_elements
5.3 Storage Order
As mentioned before, memory is organized as a single-dimensional array. Two-dimensional arrays must be treated as simple single-dimensional arrays. In assembly language to declare two-dimensional arrays, we have to arrange the arrays as single-dimensional arrays.
To do this, we have to know how to organize all elements of an array. There are two different ways to organize the elements of two-dimensional array:
Row-major order: The array is organized as a sequence of ROWS. Most of programming languages such as C follow this method.
Column-major order: The array is organized as a sequence of COLUMNS. This order is being implemented in FORTRAN.
5.3.1 Address Calculation
Assume the row and column index starts from 0. The general formula to calculate the byte address of the element [a, b] can be expressed as:
Example 9
Suppose the array size has 2 rows and 3 columns:
(0, 0)
(0, 1)
(0, 2)
(1, 0)
(1, 1)
(1, 2)
The array stored in row-major order:
(0,0)
(0,1)
(0,2)
(1,0)
(1,1)
(1,2)
0
n
2n
3n
4n 5n
Lower address
Higher address
The array stored in column-major order:
(0,0)
(1,0)
(0,1)
(1,1)
(0,2)
(1,2)
0
n
2n
3n
4n 5n
Lower address
Higher address
Example 10
This is an example of how to traverse a 2-dimensional array. On the left the pseudo-code is presented, and on the right is the corresponding assembly language implementation
Psedo-code
MIPS Assembly Language
int M[][] = new int[9][4];
for(int j=0; j<9; j++) M[j][3] = 7;
.data
M: .word 0:36
# The size-of-element = 4 bytes.
# a = j, b = 3, number-of-columns = 4 ; Then,
# the offset of M[3][4] is 4 * ( j * 4 + 3 ) = 4*(4*j) + 12
.text
la $t2, M
li $t1, 9
li $t0, 0
li $t4, 7
L2: beq $t0, $t1, X2
sll $t3, $t0, 4
addiu $t3, $t3, 12
addu $t3, $t3, $t2
sw $t4, 0($t3)
addiu $t0, $t0, 1
j L2
X2:
The solution of above example using pointer:
.data
M: .word 0:36
.text
la $t2, M
li $t1, 9
li $t0, 0
li $t4, 7 addiu $t2, $t2, 12
L2: beq $t0, $t1, X2 sw $t4, 0($t2) addiu $t0, $t0, 1 addiu $t2, $t2, 16 j L2
X2:
Instructions
Description
and
rd, rs, rt
rd = rs & rt
andi
rt, rs, immediate
rt = rs & immediate
or
rd, rs, rt
rd = rs | rt
ori
rt, rs, immediate
rd = rs | immediate
nor
rd, rs, rt
rd = ! ( rs | rt )
xor
rd, rs, rt
To do a bitwise logical Exclusive OR.
xori
rt, rs, immediate
Explanation / Answer
Exercise 3.1
1)
.data
insert_into:
.word 4
Ask_Input:
.asciiz "Please Enter a String to Print "
Tell_Output:
.asciiz "You typed in: "
.text
main:
la $a0, Ask_Input
li $v0, 4
syscall
la $a0, insert_into
la $a1, insert_into
li $v0, 8
syscall
toupper:
lb $t2, 0($a0) # $t2 = *s
beq $t2, $0, exit # $t2 = 0?
blt $t2, 97, next # $t2 < 97?
bgt $t2, 122, next # $t2 > 122?
sub $t2, $t2, 32 # convert
sb $t2, 0($a0) # and store back
next:
addi $a0, $a0, 1 # s++
j toupper
exit:
jr $ra
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.