FAQ - Introductory Assembly Programming
Q1: How can I determine the number of times a loop is executed?
Q2: How do I compute branch offsets?
Q3: How do I specify a branch if equal instruction?
Q4: How can a branch target be specified?
Q5: How do I specify a forward branch?
Q6: How do I compute the loop end test for an array?
Q7: How do I load an array's values?
Q8: How do I compute a branch offset (in bytes)?
Q9: How do I load a large (greater than 16 bits) immediate value into a
register?
Q1:
How can I check my answer for problems like how many times a loop in a
program fragment executed. My answers are not always right, and
sometimes they are off by 1. Can you explain again how to do these
kind of problem correctly. (e.g. part A from Problem 3 of the final,
Spring 99)
The best way to check these loop count problems is to test conditions
going through the loop the first time. Then determine conditions when
the loop will be exited during the last time. Determine the loop step.
Then you should be able to compute the number of loops. And be
careful; if a loop variable is 5 during the first loop, 10 during the
last loop, and the step is 1, the iterations is 6 (10,9,8,7,6,5).
Q2:
Regarding Spring 2001 final exam B, problem 3B, in one of your emails
you said to find out the branch offset take the PC of the next
instruction and find out how many steps are till the label, then
multiply by 4. In this test the bne command is at 1008 and the label
is at 1024. Isn't there only 4 steps between them making the offset
(in bytes) 16. Also in part D, how do you determine which register
contains the results computed?
This question asks about the beq instruction at address 1020. Starting
with the next instruction at address 1024, the branch needs to go back
5 instructions or 20 bytes to address 1004. The final answer is -20
bytes. Once one recognizes the operation of the code fragment, the
result can be identified. Also, $2 is computed in the loop; but never
used.
Q3:
On the final of Fall 00', the last question asks for the amount of
bits needed to specify a BRANCH IF EQUAL instruction, how should
decide it? From the answer, it coincide with the instruction word
size. If this is not an coincidence, does it mean no matter what
instruction, the amount of bits to specify it is always equal to the
instruction word size?
The branch if equal instruction requires an opcode, two registers
operands, and an immediate operand. Don't overthink "correlation
trends"; its easier to just answer the question.
Q4:
I have some questions concerning branching when written in assembly.
How does your assemble program know where and how to branch if the
branch offset is never stated? i.e. suppose we have: "BNE $1, $2" if $1
!= $2 then we will take the branch, however the direction and
magnitude of the branch is stated nowhere.
Correct; the branch target must be defined either by an
instruction branch offset (+/-32K) or by a symbolic label (so the
assembler computers the branch offset). I will tell you which format
is required.
Q5:
Suppose we want to branch 3 instructions forward, shouldn't we state
this as: "BNE $1, $2, +3"; entering 3 as an immediate value for the
branch offset? If this is not the case do you want us to write, BNE
$1, $2 (+3 instructions/+12 bytes) instead?
This is correct; but remember that the starting location is the
instruction following the branch. Your example will conditionally
branch to the fourth instruction following the BNE.
The byte branch offset is four times the instruction branch
offset. The immediate field of the branch is an instruction branch
offset.
Q6:
On the Spring of 2001 in Problem 3, the first line of the last box
of code. Can you explain where the value of 240 come from and how we
would know that it is the final array element.
The array starts at byte address 200; it is ten words long; a word is
4 bytes; 10 x 4 = 40 bytes, 200+40 = 240 = the first byte address
following the array.
Q7:
Regarding Fall 2000 final exam problem 4B, how do you know the value
of what is stored in memory[$1] and > memory[$1 + 1000]?
This code uses memory indirect plus displacement addressing. As we
discussed in class, we will restrict our study this term to register
indirect addressing only (no displacement). In this code fragment, we
do not know the actual values stored in memory.
Q8:
Regarding Fall 2000 final exam problem 4B, how do you calculate the
branch offset of an instruction at a specific address?
To compute branch offset, begin at the instruction following the
branch (remember the IP is always incremented before the branch offset
is added). Count the number of instructions (positive or negative) to
the branch target. This is the branch offset in instructions. Now
multiply by 4 to compute the branch offset in bytes.
Q9:
For part d: how does the ori instruction work can you please
explain one more time how the answer F64D0DaD.
LUI stores a 16 immediate value in the upper 16 bits of the
destination register. The lower 16 bits of the register are
cleared. ORI performs a bitwise logical or of a given register and a
16 bit immediate value (now in the lower 16 bit position). The ORI
instruction, being a logical operation, does not sign extend the 16
bit value. Since a bit ORed with 0 results in the bit (OR identity),
the two 16 bit values are effectively concatenated. Together, these
two instructions form a 32 immediate value in the specified register.