Uploaded by Zaiyan Monem

Homework 2

advertisement
Zaiyan Muhammad
EC413
Homework 2
Question 1
a) add $10,$1,$2
Legal
Evaluation: $10 = 0x0000D0A0 + 0x00000073 = 0x0000D113
b) sub $10,$2,$1
Legal
Evaluation: $10 = 0x00000073 - 0x0000D0A0 = 0xFFFF2FD3
c) and $10,0x00000345,0x00000001
Illegal (The and operation can only be performed between registers, not immediate values.)
d) lw $10,$8($7)
Legal (Cannot be evaluated without memory values.)
e) add $10,$3,-10
Legal
Evaluation: $10 = 0xFFFFFF00 + (-10) = 0xFFFFFEF6
f) or $10,$3,0x000000AB
Legal
Evaluation: $10 = 0xFFFFFF00 | 0x000000AB = 0xFFFFFFAB
g) and $10,$5,0x000000AB
Legal
Evaluation: $10 = 0x00012BAD & 0x000000AB = 0x00000029
h) xor $10,$6,$5
Legal
Evaluation: $10 = 0x0234EF01 ⊕ 0x00012BAD = 0x0235A4AC
i) sll $10,$7,3
Legal
Evaluation: $10 = 0x00007B01 << 3 = 0x0003D808
j) sra $10,$6,2
Legal
Evaluation: $10 = 0x0234EF01 >> 2 = 0x008D3BC0
k) sra $10,$3,1
Legal
Evaluation: $10 = 0xFFFFFF00 >> 1 = 0xFFFFFF80
Question 2
1. Assembly Declarations:
.data
A: .word 0x00000007 ; int A = 7; (4 bytes)
B: .byte 0x00
; char B; (1 byte)
C: .half 0x0000
; short int C; (2 bytes)
D: .ascii "string\0" ; char D = "string"; (7 bytes including null terminator)
E: .word 0x00000003 ; int E[0] = 3; (4 bytes)
.word 0x00000004 ; int E[1] = 4; (4 bytes)
2. Memory Layout (Little Endian, Starting at 1000):
- 1000-1003: 07 00 00 00 (A)
- 1004: 00 (B)
- 1005-1006: 00 00 (C)
- 1007-100D: 73 74 72 69 6E 67 00 (D, ASCII representation of "string" with null terminator)
- 100E-1011: 03 00 00 00 (E[0])
- 1012-1015: 04 00 00 00 (E[1])
Question 3
1. LA $t0,a
- Memory Operand: None
- Address: N/A
- Value Written: $t0 = 0x00
2. LW $s0,d
- Memory Operand: 0xA8
- Address: 8
- Value Written: $s0 = 0xA8
3. SB $t0,13($zero)
- Memory Operand: 0xB3
- Address: 13
- Value Written: Memory[13] = 0x00
4. LA $t3,f
- Memory Operand: None
- Address: N/A
- Value Written: $t3 = 0x0D
5. LB $t4,0($t3)
- Memory Operand: 0xB3
- Address: 13
- Value Written: $t4 = 0x00
6. SW $t0,i
- Memory Operand: 0xB6
- Address: 16
- Value Written: Memory[16-19] = 0x00000000
7. LH $t1,1($t3)
- Memory Operand: 0xB4
- Address: 14
- Value Written: $t1 = 0xB400
8. SH $t1,g
- Memory Operand: 0xB4
- Address: 14
- Value Written: Memory[14-15] = 0xB400
9. LW $t5,17($zero)
- Memory Operand: 0xB7
- Address: 17
- Value Written: $t5 = 0xB7000000
10. LBU $t5,f
- Memory Operand: 0xB3
- Address: 13
- Value Written: $t5 = 0x00
Question 4
main:
# Setup stack frame
addi $sp, $sp, -32 # Allocate space for 8 local variables (32 bytes)
# int ia = 7;
li $t0, 0x7
# Load immediate value 7 into temporary register $t0
sw $t0, 0($sp) # Store $t0 into the stack at offset 0 (ia)
# int ib = 0x23;
li $t1, 0x23
# Load immediate value 0x23 into temporary register $t1
sw $t1, 4($sp) # Store $t1 into the stack at offset 4 (ib)
# ia = 0x1234;
li $t0, 0x1234 # Load immediate value 0x1234 into $t0
sw $t0, 0($sp) # Store $t0 into the stack at offset 0 (ia)
# ib = ia;
lw $t1, 0($sp) # Load value of ia from the stack into $t1
sw $t1, 4($sp) # Store $t1 into the stack at offset 4 (ib)
# ic = ia + ib;
lw $t2, 0($sp) # Load value of ia from the stack into $t2
lw $t3, 4($sp) # Load value of ib from the stack into $t3
add $t4, $t2, $t3 # Add $t2 and $t3 and store the result in $t4
sw $t4, 8($sp) # Store $t4 into the stack at offset 8 (ic)
# id = ic | ib & 17;
lw $t4, 8($sp) # Load value of ic from the stack into $t4
andi $t3, $t1, 17 # And $t1 with 17 and store the result in $t3
or $t5, $t4, $t3 # Or $t4 and $t3 and store the result in $t5
sw $t5, 12($sp) # Store $t5 into the stack at offset 12 (id)
# ie = ~ig;
lw $t6, 20($sp) # Load value of ig from the stack into $t6
not $t7, $t6
# Not $t6 and store the result in $t7
sw $t7, 16($sp) # Store $t7 into the stack at offset 16 (ie)
# ig = (ia - ib) ^ (ic + id);
lw $t0, 0($sp) # Load value of ia from the stack into $t0
lw $t1, 4($sp) # Load value of ib from the stack into $t1
sub $t2, $t0, $t1 # Subtract $t1 from $t0 and store the result in $t2
lw $t4, 8($sp) # Load value of ic from the stack into $t4
lw $t5, 12($sp) # Load value of id from the stack into $t5
add $t3, $t4, $t5 # Add $t4 and $t5 and store the result in $t3
xor $t6, $t2, $t3 # Xor $t2 and $t3 and store the result in $t6
sw $t6, 20($sp) # Store $t6 into the stack at offset 20 (ig)
# Restore stack and return
addi $sp, $sp, 32 # Deallocate stack frame
jr $ra
Question 5
.data
a: .word 4
b: .word 30
c: .word 20
d: .word 10
.text
.globl main
main:
# Return to caller
# Load values of a, b, c into registers
lw $t0, a # $t0 = a
lw $t1, b # $t1 = b
lw $t2, c # $t2 = c
# if (a == b) c = 33;
beq $t0, $t1, a_eq_b # branch to a_eq_b if a == b
bne $t1, $t2, b_neq_c # branch to b_neq_c if b != c
# else {
# if (a > b) b = 10;
# else if (c <= 10) c = 12;
# else a = 5;
#}
bgt $t0, $t1, a_gt_b # branch to a_gt_b if a > b
ble $t2, $10, c_le_10 # branch to c_le_10 if c <= 10
li $t0, 5
sw $t0, a
j end
#a=5
# store a to memory
# jump to end
a_eq_b:
li $t2, 33
# c = 33
sw $t2, c
# store c to memory
j end
# jump to end
b_neq_c:
li $t0, 20
# a = 20
sw $t0, a
j end
# store a to memory
# jump to end
a_gt_b:
li $t1, 10
# b = 10
sw $t1, b
j end
# store b to memory
# jump to end
c_le_10:
li $t2, 12
# c = 12
sw $t2, c
# store c to memory
j end
# jump to end
end:
# Exit program
li $v0, 10
syscall
# load exit system call code into $v0
# make system call
Question 6
a) Difference between LI, LA, and LW in MIPS Assembly
1. LI (Load Immediate):

It is used to load a constant value (immediate value) directly into a register.

Example: li $t0, 5 loads the value 5 into register $t0.
2. LA (Load Address):

It is used to load the address of a variable (label) into a register.

Example: la $t0, myVar loads the address of myVar into register $t0.
3. LW (Load Word):

It is used to load a word (32-bit value) from memory into a register.

Example: lw $t0, 0($t1) loads the word from the memory address contained in
$t1 into register $t0.
I added examples so its easy for me to understand when I review them for my exam.
b) Translating C to Assembly Language
.data
VarA: .word 0
VarB: .word 0
VarC: .word 0
VarE: .word 0
VarF: .word 0
VarD: .word 10
ArrayA: .space 400 # int ArrayA[100]; 100 integers * 4 bytes each
.text
.globl main
main:
# VarA = 20;
li $t0, 20
# Load immediate value 20 into $t0
sw $t0, VarA
# Store $t0 to VarA
# VarB = VarA;
lw $t1, VarA
# Load word from VarA into $t1
sw $t1, VarB
# Store $t1 to VarB
# ArrayA[1] = VarB;
lw $t2, VarB
# Load word from VarB into $t2
sw $t2, 4(ArrayA) # Store $t2 to ArrayA[1] (4 is the offset for the second element)
# VarE = ArrayA;
la $t3, ArrayA # Load address of ArrayA into $t3
sw $t3, VarE
# Store $t3 to VarE
# VarF = &VarE;
la $t4, VarE
# Load address of VarE into $t4
sw $t4, VarF
# Store $t4 to VarF
# Exit program
li $v0, 10
syscall
# load exit system call code into $v0
# make system call
Question 7
.data
A: .word 1, 2, 3, 4, 5 # Example values for array A
B: .space 20
# Allocate space for array B (5 integers * 4 bytes each)
.text
.globl main
main:
# Initialize $t0 to 0 (it will be used as the offset)
li $t0, 0
# Initialize $t1 to 20 (5 elements * 4 bytes each)
li $t1, 20
loop:
# Check if we have transferred all elements
beq $t0, $t1, end
# Perform the data transfer: B[$t0] = A[$t0]
lw $t2, A($t0) # Load word from A[$t0] into $t2
sw $t2, B($t0) # Store word from $t2 to B[$t0]
# Increment the offset by 4 (size of an integer)
addi $t0, $t0, 4
# Jump back to the start of the loop
j loop
end:
# Exit program
li $v0, 10
syscall
# load exit system call code into $v0
# make system call
Comments: In this code, $t0 is used as the offset register, $t1 is used to store the total size of
the arrays in bytes, and $t2 is used as a temporary register to hold the value being transferred
from array A to array B. The loop continues until $t0 equals $t1, at which point all elements
have been transferred, and the program exits.
Question 8
a) Negate $s0:
sub $s0, $zero, $s0
b) Take the Two's Complement of $s1:
sub $s1, $zero, $s1 # Negate $s1
addi $s1, $s1, 1
# Add 1 to $s1
c) Set bits 7, 14, 15, 26, and 29 in $s2:
ori $s2, $s2, 0x0800 # Set bit 7
ori $s2, $s2, 0xC000 # Set bits 14 and 15
lui $t0, 0x2400
# Load upper immediate to set bits 26 and 29
or $s2, $s2, $t0
# OR $s2 with $t0
d) Clear bits 2, 5, and 11 in $s3:
andi $s3, $s3, 0xFFFFF7B3 # Clear bits 2, 5, and 11
e) Branch if and only if bits 1, 12, and 13 of $s4 are set:
andi $t0, $s4, 0x3002 # AND $s4 with mask to isolate bits 1, 12, and 13
beq $t0, 0x3002, Label # Branch if the result is equal to 0x3002
f) Shift $s5 to the right by 6 while preserving the sign:
sra $s5, $s5, 6
g) Move bits 5, 6, and 7 of $s6 to bits 0, 1, and 2 while clearing all other bits:
srl $s6, $s6, 5
# Shift right logical by 5 positions
andi $s6, $s6, 0x7 # Clear all other bits
Download