Title: Assembly Language Programming and Introduction to WRAMP
1Assembly Language Programming and Introduction
to WRAMP
2Introduction
C
- In previous lectures we have been looking at how
data is stored in a computer - We will now take a look at how a computer
operates - In Stored Program computers, the most common
type, the computer executes a program created
by the programmer - The level of abstraction where we can best
examine the workings of the computer is at the
assembly language level - E.g. add 3,4,5
assembler
Machine code
CPU
3Why Assembly
- Assembly language is used for
- Hand optimisation of commonly used code (now rare
as modern optimising compilers are very good) - Machine dependent parts of operating systems.
- Device drivers
- Some embedded devices with limited memory and
simple functionality. - Most programmers do not use assembly. It is
taught to provide understanding of the CPU.
4Binary vs. assembler
- The computer itself operates on binary numbers.
- Programming in binary is extremely difficult to
ease this, assembler language was invented. - Assembler uses mnemonics to aid the programmer
an additional step is then required, to translate
the assembler into binary for the computereach
CPU is different
MOV AL,0200 becomes A0 0002 (8086
assembler)
Address (LSB first)
Op code
5Components of a Programming Language
- All programming languages must be able to specify
four types of operations - Movement of data
- Must be able to handle various data types
- Arithmetic operations
- e.g. sum a b - c
- Conditional execution
- e.g. while (count 1 count lt 10 count)
if (a lt b) cc1 else cc-1 - Input and Output.
- e.g. Printf (Total i\n,sum)
6Why WRAMP?
- Most Computer Instruction sets are optimised for
performance or backwards compatibility - Tends to make them very complex and hard to learn
- WRAMP was designed specifically for teaching
purposes - Allows us to focus on the main concepts rather
than a particular manufacturers quirks - Very few people will program extensively in
assembler but it is studied so that you can
better understand CPU operation.
7WRAMPWaikato RISC Architecture Microprocessor
- Features
- RISC architecture
- 32 bit data paths
- Load/Store memory architecture limited
addressing modes - Three operand format for instructions
- Regularity of instructions
- Described in VHDL, and implemented in Xilinx
FPGA device.
- A part of REX, a laboratory exercise machine with
WRAMP processor. - Connected to workstation in labs.
- Displays for contents of buses.
- More see Exercise 2 (on web page 24/3).
8WRAMP
- Will use a number of C examples to introduce
WRAMP, so the desired operation is clear. - Starting with arithmetic operations (,-,,/)
- Generally an assembly language instruction can
carry out a single operation, that is - There is a 11 mapping between assembler
instructions and their binary equivalents
Exact syntax depends upon processor
sum a b
C
9Consider
C
sum a b - c
- Where are the variables stored?
10Instruction Sizes
- Consider the amount of memory to store the
instruction
add a,b,c
- Number of bits to store an opcode dependent on
number of instructions in instruction set---
e.g. 8 bits gt 28 or 256 instructions (max)and
6 bits gt 64 instructions - Operand fields contain addresses of variables
being accessed (32 bits per operand) - Therefore, instruction size 8 (3 32) 102
bits - Clearly, something must be done to limit this
11Instruction Sizes (continued)
- To execute this instruction would require
- 4 memory references to fetch instruction
- 3 memory references to execute instruction
- Several techniques are used which minimize this
problem among them - Use small, local memory (e.g. registers) to store
variables - Use registers as pointers to memory, instead of
directly addressing memory.
12Registers
- NOTE Operations inside a processor are
significantly faster than those outside, so an
operand stored in local memory (registers) can be
accessed more quickly than an operand in main
memory. - Registers analogous to a Nickname(E.g.
Palmerston North often referred to as Palmy) - Have to be careful to avoid running out of this
local memory also ambiguity is possible
(registers are used for storing many variables at
different times during program execution)
13Registers
- Possibilities
- Identify locations referenced frequently and give
them a shorter address (sometimes within a given
page of memory) - create a second small memory (called a register
file) to store frequently accessed variables - e.g. 16 word register file requires 4 bit address
for any given register, instead of the 32 bits
earlier required. - Use register contents to point to address within
memory, where desired quantity is stored.
Sometimes used in conjunction with an offset,
allowing access to a block of memory, where a
group of data items is stored.
14Using registers as pointers to memory
Register 1
32 bits
Points to a memory location
4 bits
More registers-16 total
Points to a register
All of memory 232 locations
Desired contents
15Registers (continued)
- i refers to i th word of this memory
- Instruction add a, b, ccould be represented as
add 1, 2, 3 - As predetermined then only need to store 4 bits
for each operand, the name of the register where
it is stored. - NOTE values of a,b,c, are encoded in
instruction i.e. the instruction may be
different for add 1, 2, 3 and for add 4, 5,
7
0000000000000000
Rt
0000
Rd
Rs
Register number (0-15)
16Registers
31 0
0
0
Always 0
1
2
3
- As an example, WRAMP has a register file.
- This register file is small enough to be stored
inside a modern CPU - Further reduces number of main memory references,
with corresponding speed increase.
4
5
6
7
Available for general use
8
9
10
11
12
13
sp (14)
Stack pointer
ra (15)
Ret address
17Registers
31 0
0
0
- The WRAMP register file has 13 general purpose
registers which can be used for whatever
temporary storage you need in your program - But you need to keep track of what variable is
stored where! So, make a copy of this page, and
allocate your storage space
1
2
3
4
5
6
Available for general use
7
8
9
10
11
12
13
sp (14)
ra (15)
18Monitor Commands
- When running the REX boards, WRAMPmon is the
program that interprets your keystrokes into
WRAMP actions. The monitor program only responds
to certain commands (see web page resources for
the manual) - The commands are repeated here, for convenience.
- load
- go address
- cont
- dis start addr end
- vm
- vr reg
- sr ltreggt ltvaluegt
- sb ltaddrgt
- vb
- rb ltaddrgt
- s
- so
- help or ?
19Troubleshooting WRAMP
- Your major assets for troubleshooting WRAMP are
- A register map
- A list of your program
- Breakpoints (which you set/reset)
- Viewing registers
- Program knowledge
WRAMP Troubleshooting Aid WRAMP Troubleshooting Aid WRAMP Troubleshooting Aid
PC 0 1 2 3
4 5 6 7
8 9 10 11
12 13 sp ra