0% found this document useful (0 votes)
34 views21 pages

lab4 (2)

...ơ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views21 pages

lab4 (2)

...ơ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Lab4

Full in name: Thái Đinh Trúc Group:


Student ID: 23133081

Part 4.1 Memory in ARMLite


On the right side of the ARMLite simulator is a grid of memory addresses, with each block
of 8 hex digits (all initialised to 0) representing a 32 bit word.
Click on any visible memory word and type in 101 (followed by the "Enter" key).

4.1.1 What value is displayed ? Why ?


Click on another memory word, enter 0x101

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.

4.1.2 What value is displayed, and why?


On another memory word, enter 0b101

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.

Part 4.2 Memory Addressing


Each word of memory has a unique ‘address’, expressed as a five-digit hex number. On
the ARMLite Simulator, memory words are laid out in four columns, however this is only
for visual convenience. Notice that each row starts with a four digit hex value (in white)
that looks like this:
These values represent the first four digits of the address for all memoery words in that
row.
Now look along the top of each column in the Memory grid and note these values:

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.

Part 4.3: Editting and Submitting Assembly Code


On the left side of the ARMlite simulator is the Program window. This is where you can
load and/or edit assembly code to be executed by the simulator.
Click the Edit button below the Program window, and then copy and Paste the following
ARMLite assembly program into the window:

MOV R1, #.PixelScreen


MOV R2, #screen2
MOV R6, #0
MOV R9, #.black
MOV R10, #.white
MOV R3, #0
loopWhite: STR R10, [R2+R3]
ADD R3, R3, #4
CMP R3, #12288
BLT loopWhite
MOV R3, #260
randLoop: LDR R0, .Random
AND R0, R0, #1
CMP R0, #0
BNE .+2
STR R9, [R2+R3]
BL nextCell
CMP R3, #12032
BLT randLoop
copyScreen2to1: MOV R3, #0
copyLoop: LDR R0, [R2+R3]
STR R0, [R1+R3]
ADD R3, R3, #4
CMP R3, #12288
BLT copyLoop
ADD R6, R6, #1
MOV R3, #260
nextGenLoop: MOV R5, #0
SUB R4, R3, #256
BL countIfLive
SUB R4, R3, #252
BL countIfLive
ADD R4, R3, #4
BL countIfLive
ADD R4, R3, #260
BL countIfLive
ADD R4, R3, #256
BL countIfLive
ADD R4, R3, #252
BL countIfLive
SUB R4, R3, #4
BL countIfLive
SUB R4, R3, #260
BL countIfLive
CMP R5, #4
BLT .+3
STR R10, [R2+R3]
B continue
CMP R5, #3
BLT .+3
STR R9, [R2+R3]
B continue
CMP R5, #2
BLT .+2
B continue
STR R10, [R2+R3]
continue: BL nextCell
MOV R0, #12032
CMP R3, R0
BLT nextGenLoop
B copyScreen2to1
countIfLive: LDR R0, [R1+R4]
CMP R0, R10 //White
BEQ .+2
ADD R5, R5, #1
RET
nextCell:
ADD R3, R3, #4
AND R0, R3, #255
CMP R0, #0
BEQ .-3
CMP R0, #252
BEQ .-5
RET
HALT
.ALIGN 1024
screen2: 0

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

 The total number of instructions that end up as words in memory? (Why?)


o 44 because each instruction occupies exactly one word
Click Edit again and remove the comma from the first line of code. What happens when
you Submit now?
OK - enough mucking about. Restore the program to its original condition, either by going
back to Edit, or just copying it again, and click Submit. Time to run this thing!
MY ANSWER:
1. Memory Addresses in ARMLite:
o In ARMLite, each instruction in the program is stored at a specific location in
memory.
o Memory addresses are typically represented in hexadecimal format for
easier reading and management.
2. Displaying Addresses on Hover:
o When hovering over a line of source code, ARMLite displays the memory
address corresponding to that instruction.
o This helps programmers know the exact location of the instruction in
memory, assisting in the debugging and optimization process.
3. Why Addresses Have 5 Hexadecimal Digits:
o ARM memory uses a 32-bit address space, allowing addressing from
0x00000000 to 0xFFFFFFFF.
o However, in many cases, actual addresses only use a portion of this space.
o Displaying 5 hexadecimal digits from 0x00000 to 0xFFFFF is sufficient to
cover the address range of many small programs, making the interface
cleaner and easier to follow.
Thus, the value of 5 hexadecimal digits that appears when hovering over lines of source
code in ARMLite represents the memory address where the corresponding instruction is
stored. This display of addresses helps in tracking and managing the program effectively.
--When you remove the comma from the first line of code and click Submit, it may
generate a syntax error depending on the language and context, as commas are often
required for correct syntax in lists, parameter definitions, or specific statements.
Part 4.4 Executing and Debugging Assembly Code
To execute the assembled source code, we need to click the Run button, circled below:
Run.PNG

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.

Run the program once more.

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

Performs a bit-wise logical AND on the two input values,


AND AND R2, R1, #4 storing the result in the equivalent bit of the destination
register.

ORR ORR R1, R3, R5 As above but using a bit-wise logical OR

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.

‘Logical Shift Right’. Shifts each bit of the input value to


the right, by the number of places specified in the third
LSR LSR R1, R1, R2
operand, losing the right-most bits, and adding zeros on
the left.

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

Decimal value of the destination Binary value of the destination


Instruction register after executing this register after executing this
instruction instruction

MOV 50 0b110010

AND 2 0b10

ORR 58 0b111010

EOR 17 0b10001

LSL 1600 0b11001000000

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:

Task 4.5.6: Let's play again !


Your six initial numbers are: 99, 77, 33, 31, 14, 12 and your target number is: 32
When the program is complete, take a screen shot of the code and the register table
and paste into your submission document.
MY ANSWER:
Part 4.6 Signed Integers
Copy and Paste the following code into the ARMlite code editor and submit the code.
MOV R0, #9999
LSL R1, R0, #18
HALT
Before executing, switch ARMlite to display data in memory in Decimal (signed) using the
drop down box below the memory grid.
Now run the program and note the result in register R1.
4.6.1 - Why is the result shown in R1 a negative decimal number, and with no
obvious relationship to 9999 ?
Hint: Mouse over the values in R0 and R1 and take a look at the binary strings.
Switch ARMlite to display in Binary format using the dropdown box under the memory grid.
You can’t edit register values directly, but you can edit memory words. Click on the top-left
memory word (address 0x00000) and type in the following values, which will be interpreted
as decimal and translated into the 32-bit two’s complement format, which you can then
copy back into your
answers.
MY ANSWER:
 Explanation of the Instructions:
o MOV R0, #9999: This instruction assigns the value 9999 to register R0.
o LSL R1, R0, #18: This instruction performs a left shift on R0 by 18 bits and
stores the result in R1.
 Explanation of the Result:
o A left shift (LSL) of 18 bits is equivalent to multiplying the initial value of R0
by 2182^{18}218.
o Given the initial value R0=9999R0 = 9999R0=9999, after the left shift, it will
become 9999×218=9999×2621449999 \times 2^{18} = 9999 \times
2621449999×218=9999×262144.
 Phenomenon of Negative Numbers:
o In a 32-bit system, if the computed result exceeds
2,147,483,6472,147,483,6472,147,483,647 (the limit for positive 32-bit
signed integers), the highest bit will become 1, causing ARM to interpret this
value as a negative number in two's complement representation.
o In this case, the binary value of 2,621,842,2562,621,842,2562,621,842,256
is 10011100001110010100000000000000, with the highest bit (bit 31) being
1. Therefore, ARM interprets this as a negative number.

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.

You might also like