lab4 (2)
lab4 (2)
MY ANSWER:
-- When entering 0x101 (hexadecimal), the displayed value is shown as
below. This occurs because the hexadecimal value is directly stored in the
memory cell corresponding to 257 in decimal.
MY ANSWER:
When entering 0b101, the displayed value is shown as in the image. The prefix 0b
indicates binary, and 0b101 in binary is equivalent to 5 in decimal, which is displayed as
0x5 in hexadecimal
4.1.3 What value is displayed, and why?
If you now hover (don’t click) the mouse over any of the memory words where you have
entered a value you will get a pop-up ‘tooltip’.
What does the tooltip tell you?
The value in decimal and binary
Below the grid of memory words is a drop down menu that looks like this:
This drop-down selector allows you to change the base in which data is displayed.
Changing the base does not change the underlying data value, only the base number
system in which the value is displayed.
Change this to Decimal (unsigned) and note the change that has occurred to the three
memory words you previously entered.
When you mouse over one of these words, what now appears in the tooltip?
The value in hex and binary
4.1.4: Does changing the representation of the data in memory also change the
representation of the row and column-headers (the white digits on a blue
background)? Should it ?
MY ANSWER:
1. Memory and Data Representation:
o In the ARMLite emulator, data can be displayed in various number bases
(such as decimal, hexadecimal, binary).
o Changing the base of representation only alters how data is displayed in
memory cells and does not change the actual value stored in memory.
2. Row and Column Headers:
o The row and column headers (the white numbers on a blue background)
represent memory addresses and are not the data stored in the memory
cells. They indicate the addresses of the cells, displayed in hexadecimal
format.
o Because the row and column headers indicate addresses rather than data,
they are not affected when the user changes the display format of the data in
the memory cells.
Conclusion:
Changing the data representation in memory cells does not alter the representation
of the row and column headers.
Reason: The row and column headers are fixed components representing
addresses and are always displayed in hexadecimal format for easy differentiation,
regardless of the base in which the data is displayed.
These single digit hex values represent offsets from the row-header address. Therefore,
the full address of any memory word is obtained by appending the column header digit to
the 4 digit row-header. For example, the address of the top-left word on this screen is
0x00000, and the bottom-right is
0x001fc.
4.2.1 Notice these column header memory address offsets go up in multiples of
0x4. Why is this ?
Hint: remember how many bits are in each memory word !
ARMlite, in common with most modern processors uses ‘byte addressing’ for memory.
When storing or retrieving a word you generally specify only the address of the first of the
bytes making up each word - we'll come back to this when we start dealing with storing
and loading values to and from memory.
MY ANSWER:
Memory address offsets in the column headers increase in multiples of 0x4
because:
Each memory word in ARMLite has a size of 32 bits (or 4 bytes). When using
addresses, each memory word occupies 4 bytes, which means the address of each
word will be spaced 4 bytes apart in memory. In hexadecimal, 4 is represented as
0x4.
Therefore, the offsets increase in multiples of 0x4 (0x0, 0x4, 0x8, 0xC).
This ensures that each 32-bit word is stored and accessed correctly according to its
alignment in memory.
Once copied, click the Submit button. This invokes the assembler and all going well,
should not give any errors. If it does then you may need to check you correctly copied all
the code above (nothing more, nothing less).
4.3.1 Take a screen shot of the simulator in full and add it to your submission
document
Notice that the memory window has changed ? You should see lots of values in at least
the first 13 rows of memory words.
4.3.2 Based on what we've learnt about assemblers and Von Neuman architectures,
explain what you think just happened.
You will also see that ARMlite has now added ‘line numbers’ to your program. These do
not form part of the source code, but are there to help you navigate and discuss your code.
Hover the mouse over one of the lines of the source code (after the code has been
submitted).
You will see a pop-up tooltip showing a 5 digit hex value.
MY ANSWER:
1. Phenomenon After Submitting Source Code:
When you submit the source code in the ARMLite program window and there are no
errors, the code is compiled into machine code and loaded into memory.
Signs to Recognize: The memory window will display different values, changing
from the initial state of all zeros. The new values appearing are the machine code
corresponding to the compiled instructions.
Additionally, you will see line numbers added to the left of the instruction lines to aid
in easier tracking. These line numbers are not part of the source code but serve as
guides for navigating and discussing the code.
2. Detailed Explanation of What Happened:
When you submit the code, the compiler converts the assembly code into
corresponding 32-bit binary values (machine code) for each instruction. These
values are loaded into memory so that the processor can read and execute them.
Result in the Memory Window: The first lines of memory will contain the machine
code for each instruction from the program, with each instruction occupying one
memory word (4 bytes).
Appearance of Line Numbers: The line numbers that appear after submitting the
code help you easily locate instructions in the source code when performing tasks
such as editing, debugging, or discussing the code.
4.3.3 Based on what we have learnt about memory addressing in ARMlite, and your
response to 4.3.2, what do you think this value represents ?
If you're not sure what's going on here, ask your tutor for assistance.
It's time to try a few other things.
Hit Edit and try inserting:
A couple of blank lines
Additional spaces before an instruction, or just after a comma (but not between
other characters)
A comment on a line of its own, starting with // such as //My first program
A comment after an instruction but on the same line
Submit the code again.
What has happened to:
The blank lines
o Its be removed
Additional spaces
o Its be removed
The comments
o Cmt showed in green but note affect the outcome and the memory
The line numbers
o Cmt showed in green but note affect the outcome and the memory
o
You’ll see a spinning gearwheel appear near the run controls to indicate that the processor
is active.
You will also observe a lot of activity in the ‘graphics screen’ (the lowest of the three panes
under
Input/Output). After a short while (a few seconds to a couple of minutes) the display will
stabilise.
The program you have loaded and just run is a simulation of a colony of simple organisms,
being born, reproducing and, eventually dying. (Individual cells never move, but the
patterns of cells being born and dying give the impression of movement, and many
interesting dynamic patterns emerge). The code is a variant of a very famous program
called Life (see Conway's Game of LifeLinks to an external site. for more information).
To stop the program, you can click on the square Stop button in the above image. To run
the program again, simply click Run .
You will notice the behaviour of the program is different each time you run it - this is
because the starting pattern of cells is randomised.
While the program is executing, click the Pause button (between the Run and Stop
buttons).
As well as freezing the graphics screen, you will also see orange highlighting appear in
both the Program and Memory windows.
4.4.1 What do you think the highlighting in both windows signifies ?
You can continue execution by pressing Play again. Do this and then click Pause again.
Now click the button circled in red below.
MY ANSWER:
The button circled in red represents a function that allows for the execution of code one
instruction at a time. When clicked, this button accelerates the process of executing each
instruction and updates the processor's status accordingly.
The button circled in black allows the program to run at a slower, automatic speed, making
it easier to observe changes in the processor's status in real-time.
4.4.2 What do you think happens when you click the button circled in red ?
Now click the button circled in black and notice what happens.
You will hopefully notice that the program resumes execution, but at a substantially slower
pace than before. You will also notice the orange highlighting in the Program window
stepping through lines of code.
Now click the same button again and see what happens. You will hopefully see that when
you click the button a few times in succession, the execution speed increases.
These two buttons allow you to slow things down to literally, in the case of the red circled
button, single steps of code execution. This is invaluable for debugging code, particuarly
when you want to check whether the outcome of a given instruction has produced what
you expect (be that a value in memory, in a register, or a graphical element on the display
etc).
FInally, while paused, click line number 21 of the source code in the Program Window.
This will paint a red background behind the line number like this:
This is called ‘setting a break point’ and will cause processing to be paused when the
breakpoint is reached.
Having set the breakpoint, continue running until the pause is observed (almost
immediately!).
MY ANSWER:
When I press that button, it means that the program will execute each
instruction sequentially. Each time I press that button, it represents a "Step,"
advancing to the next instruction in the source code, allowing you to observe
and analyze each step of the code's operation.
After I press the black button, which indicates "slow," the program will run an infinite loop in
loopwhite without stopping.
4.4.3 Has the processor paused just before, or just after executing the line with the
breakpoint ?
From the breakpoint you will find that you can single-step, or continue running slowly or at
full speed.
While paused you can remove a breakpoint by clicking on the line again.
Breakpoints are essential for debugging because they allow you to pause the program at
strategic points and inspect the current state, making it easier to identify issues. When
paused, you can step through the code, continue running at a slower speed, or resume
execution at maximum speed. To remove a breakpoint, simply click on the line again, and
this action will clear the red background.
Part 4.5 Registers and Basic Operations
Registers are fundamental to how data is stored and manipulated within a CPU. In the
early part of this unit we learnt how registers are generally built from banks Flip Flops,
each storing a single bit. Here we see how registers serve the needs of the programs
we seek to write and execute.
The ARMlite simulator provides 16 registers, including 13 so called general purpose
registers which can be used within the programs we write.
The general purpose registers are labelled, R0-R12 as shown in the image below.
As with main memory storage in ARMlite, registers each hold 32 bits, with the value in
each register represented by an 8 digit hexidecimal value.
In lectures we introduced the MOV instruction for storing values in registers. For example:
MOV R1, #15
takes the decimal value 15 and stores it in register R1.
MOV R2, R1
takes the value stored in R1 and copies it to register R2.
We also introduced some basic arithmetic operations like ADD (for adding) and SUB (for
subtracting).
We are not going to write a game in assembly language (yet!), but rather, we are going to
play a game that involves assembly programming.
Task: You are given 6 input values and a target value. Your task is to write a simple
assembly program that implement a mathematical equation involving only these 6 input
values, and the three instructions MOV, ADD and SUB, such that the result is as close as
possible to the target value.
Here is an example to get you started. Your initial input numbers are 100, 25,8,4,3,1 and
your target is 84. This is pretty straight forward from a mathematical perspective:
1+8+100-25 = 84
A possible implementation of this equation in ARM assembly code would be:
MOV R0,#1
ADD R1,R0,#8
ADD R2,R1,#100
SUB R3,R2,#25
HALT
Clear the Program window in ARMlite and type in the lines of code above. When done,
Submit the code ready for execution (and if any errors occur, check your syntax matches
the above).
Now, using the Step button (rather than the Run button), execute the first instruction, MOV
R0, #1
Verify R0 contains the value 0x00000001
The second instruction is ADD R1,R0,#8.
4.5.1 Before executing this instruction, describe in words what you think this
instruction is going to do, and what values you expect to see in R0 and R1 when it
is complete ?
Now execute the instruction and verify whether the output matches your expectation.
Do the same for the remaining instructions.
MY ANSWER:
MOV R0,#1
ADD R1,R0,#8
ADD R2,R1,#100
SUB R3,R2,#25
HALT
Let’s explain the function of the MOV, ADD, SUB, and HALT instructions in detail:
The MOV instruction assigns the value 1 to register R0 (i.e., R0 = 1).
The ADD instruction adds a value and stores the result in the previous register. For
example, with the instruction ADD R1, R0, #8, if R0 is set to 1, it will perform the
addition 8+18 + 18+1 and store the result in register R1, resulting in R1 = 9.
The SUB instruction is for subtraction. After performing a second ADD instruction,
let's say register R2 contains the value 109. The SUB operation would then be SUB
R3, R2, #25, which computes 109−25=84109 - 25 = 84109−25=84 and assigns the
result to R3 (i.e., R3 = 84).
The HALT instruction stops the program execution, preventing any further
instructions from being executed.
In summary, these instructions allow you to manipulate values in registers through
assignments, arithmetic operations, and control flow within a program.
4.5.2 When the program is complete, take a screen shot of the register table
showing the values.
You will have noticed the HALT instruction, which does the obvious task of halting the
program. This instruction is important for telling the program counter (which
automatically steps through the program instructions one-by-one to cease doing so.
Without this instruction, program execution would simply continue to sequentially read and
execute 32 bit words of memory beyond the last address of the loaded program code. In
practise, this will most likely result in the program simply not working, however on a real
microprocessor, can be potentially dangerous due to the possibility of executable code
residing in memory from previously loaded code - this can result in unpredictable program
behaviour.
-Lệnh đầu tiên:
-Lệnh thứ 2:
-Lệnh thứ 3:
-Lệnh thứ 4:
-Lệnh thứ 5:
4.5.3 Task: Your 6 initial numbers are now 300, 21, 5, 64, 92, 18. Write an
Assembly Program that uses these values to compute a final value of 294 (you
need only use MOV, ADD and SUB). Place your final result in register R4 (don't
forget the HALT instruction)
When the program is complete, take a screen shot of the code and the register
table.
In this week's lectures we also introduced a small set of so-called Bit-wise instructions,
designed to manipulate bits within a register in specific (and highly useful ways).
Recall the following:
Instructio
Example Description
n
EOR EOR R1, R1, #15 As above but using a bit-wise logical ‘Exclusive OR’
‘Logical Shift Left’. Shifts each bit of the input value to the
left, by the number of places specified in the third
LSL LSL R1, R1, #3
operand, losing the leftmost bits, and adding zeros on the
right.
MY ANSWER:
MOV R0, #300
SUB R1, R0, #5
SUB R2, R1, #1
MOV R4, R2
HALT
Instruction Decimal value of the destination register after executing this
instruction
MOV 300
SUB 295
SUB 294
MOV 294
HALT exit
4.5.4 Task: Write your own simple program, that starts with a MOV (as in the
previous example) followed by five instructions, using each of the five new
instructions listed above, once only, but in any order you like – plus a HALT at the
end, and with whatever immediate values you like.
Note: Keep all your immediate values less than 100 (decimal). Also, when using LSL, don’t
shift more than, say #8 places. Using very large numbers, or shifting too many places to
the left, runs the risk that you will start seeing negative results, which will be confusing at
this stage. (We’ll be covering negative numbers in the final part of this chapter.)
You may use a different destination register for each instruction, or you may choose to use
only R0, for both source and destination registers in each case - both options will work.
Enter your program into ARMlite, submit the code and when its ready to run, step
through the program, completing the table below (make a copy of it in your
submission document)
MY ANSWER:
Hàm
MOV R0, #50
AND R1, R0, #10
ORR R2, R0, #40
EOR R3, R0, #35
LSL R4, R0, #5
LSR R5, R0, #3
HALT
MOV 50 0b110010
AND 2 0b10
ORR 58 0b111010
EOR 17 0b10001
LSR 6 0b110
Task 4.5.5 Lets play the game we played in 4.5.3, but this time you can use any of
the instructions listed in this lab so far (ie,. MOV, AND, OR, and any of the bit-wise
operators).
Your six initial numbers are: 12, 11, 7, 5, 3, 2 and your target number is: 79
When the program is complete, take a screen shot of the code and the register table
and paste into your submission document.
MY ANSWER:
4.6.3 - What is the binary representation of each of these signed decimal numbers:
1, -1, 2, -2
MY ANSWER:
- number 1: 0000 0000 0000 0000 0000 0000 0000 0001
- number -1: 1111 1111 1111 1111 1111 1111 1111 1111
- number 2: 0000 0000 0000 0000 0000 000 0000 0010
- number -2: 1111 1111 1111 1111 1111 1111 1111 1110
What pattern do you notice ? Make a note of these in your submission document
before reading on.
The ARMlite simulator is using 2's Compliment to represent signed integer values. Recall
from lectures how 2's Compliment works to get the negative version of a number:
1. invert (or ‘flip’) each of the bits
2. Add 1
-- When converting a number to its negative form in binary, we flip all the bits
(changing 1s to 0s and 0s to 1s) and then add 1 to the result.
-- For example, to convert the number 2 to -2:
The binary representation of 2 is 0000 0010.
Flipping all the bits gives us 1111 1101.
Adding 1 results in 1111 1110.
4.6.4 - Write an ARM Assembly program that converts a positive decimal integer into
its negative version. Start by moving the input value into R0, and leaving the result
in R1.
Hint: the ARM instruction MVN, which works like MOV but flips each bit in the destination
register, may be useful for this !
Take a screen shot showing your program and the registers after succesful
execution, and paste into your submission document.
Using the program you wrote above, enter the negative version of the number you
previously tested as the input (ie use the output of the previous test as the input).
What do you notice ?
All things working as they should be, you should see that the exact same 2's Compliment
conversion works in both directions (ie positive to negative, and negative to positive).
MY ANSWER:
--When taking the result of the negative number of a positive input entered into R0 as the
new input, the result in R1 will be the positive equivalent of that negative number.
-- By using the method of flipping all bits and adding 1, the program will convert a positive
number to its negative counterpart and vice versa.