CS代考 DATA 33 ; initial 33 DATA 0 ; initial 0

5. Assembly Language Programming

Source and Object Programs
• A source program is a program written in a HLL or assembly language, input to a compiler or assembler.

Copyright By PowCoder代写 加微信 powcoder

• An object program is a program translated into machine code by the compiler or assembler
• An assembler or compiler generates the necessary content for all memory locations required for program or data.
– This is a list of binary words and their addresses, called a memory image, which can be stored e.g. on disk and loaded into memory when the object program is to be run.
– Program (machine code) and data should normally be kept in separate blocks of memory. A program should never write to a block of memory containing machine code.
– Some areas of memory may not be needed and are unused. These are not included in the image and are unchanged when it is loaded.
– When instructed the system loads the image into memory and sets the PC to the start address (this will be recorded as metadata along with the image). Execution of the object program then begins.
Addresses increasing
Systems and Networks 5. Assembly Language Programming
Memory Image

How Programs are Executed
• A running computer is always executing instructions. If the CPU stops executing instructions it is said to be in a halt state and does nothing.
– A modern PC is almost never in a halt state unless it is asleep or has crashed.
• To get a CPU out of a halt state it needs to be given an external signal such as
a reset or interrupt. This is done via a hardware signal.
• On a reset for example, the CPU will immediately load its program counter to some predetermined address called the reset vector where it will start executing code called the reset handler
– if there is no machine code at the reset vector, the machine will crash.
• On most systems the reset handler routine is in ROM and so always present.
Usually it begins the loading of the operating system (OS).
• The operating system runs when nothing else is running.
• The simplest operating systems, sometimes called monitors, just keep checking some input device (like a keyboard) and wait for a user to enter a text command which is then interpreted.
Systems and Networks 5. Assembly Language Programming 3

Execution in Sigma16
• The Sigma16 computer is a simulated or virtual computer and it has its own simple operating system as well as a set of user tools.
• These tools allow the user to load an image file into the (simulated) memory and have the (simulated) CPU execute it.
– On Sigma16 the image is always loaded with the assumption that execution will begin at address 0;
– There has to be valid machine code at address 0 or an error will be reported.
– Even if there is machine code, it must be part of a coherent program or the machine will behave unpredictably and eventually crash
– On a real machine, the user would have control over where the execution should begin.
• Execution continues until the CPU encounters a TRAP R0,R0,R0 instruction which causes it to stop and return control to the Sigma16 OS.
Systems and Networks 5. Assembly Language Programming 4

Compiling HLLs
• Every High level language (HLL) statement is converted into assembly language by the compiler.
– If it can be done by a compiler it can be done by a human.
– Sometimes a human can do a better job than a compiler but it is a very labour intensive task.
– It is however a very instructive one.
• Let’s look at some examples.
• Consider an assignment statement such as:
• We start by assuming that a, b, c and x are all memory locations. They can be set up by DATA statements at the end of the code such as
a DATA 0 ;Declares label a, initialises to 0
• Since we need to do arithmetic on these variables, we must load them into registers. Any registers will do but let’s choose R1, R2 and R3.
LOAD R1,a[R0] ;R1 = a
LOAD R2,b[R0] ;R2 = b
LOAD R3,c[R0] ;R3 = c
MUL R4,R2,R3 ;R4 = b*c
ADD R4,R1,R4 ;R4 = a+(b*c) STORE R4,x[R0] ;x = a+(b*c)
Systems and Networks
5. Assembly Language Programming 5

A Complete Program
LEA R1,3[R0] LOAD R2,x[R0] MUL R3,R1,R2 STORE R3,y[R0] TRAP R0,R0,R0
; terminate
Comments (;)
Full line comment (;)
; Variable declaration & initialization:
DATA 33 ; initial 33 DATA 0 ; initial 0
Systems and Networks
5. Assembly Language Programming 6
Assembler directives. Each DATA statement reserves a memory location, gives it a label and initialises it to the value on the right
Programmer can write this as $0021, $21 or just 33. All are equivalent

Assembler Listing
Line Addr Code Source statement
LEA R1,3[R0] LOAD R2,x[R0] MUL R3,R1,R2 STORE R3,y[R0] TRAP R0,R0,R0
;Variable declaration and initialisation x DATA 33
1 2 3 4 5 6 7 8
0000 0002 0004 0005 0007 0008 0008 0009
Addr Symbol
0008x 0009y
[6] [] [7] []
Location of word
Object code Source code

Statement Translation
For each type of statement in the HLL, there is a standard implementation technique using machine code/assembler.
We’ll look at 2 methods for translating a full HLL program: Statement-by-statement style.
– Every statement in the HLL program is translated in full, all by itself, into a block of Assembly Language instructions.
– Each block of instructions begins by loading the variables it needs from memory, and finishes by storing any modified variable values back into memory.
– The HLL statement is used as a full-line comment before the block of instructions, and each individual instruction has a comment describing what it does.
– This is straightforward and clear but can result in some inefficiency. Register-variable style is like statement by statement style, except
– Keep commonly used variables in registers
– Make a table showing which register contains which variable & include as a comment
– Omits unnecessary loads & stores, making the program shorter and faster
Systems and Networks 5. Assembly Language Programming 8

Example: Statement by Statement Style
LEA R1,50[R0]
STORE R1,x[R0]
; y = 2*z;
LEA R1,2[R0]
LOAD R2,z[R0] MUL R3,R1,R2 STORE R3,y[R0]
; x = x+1+z;
LOAD R1,x[R0]
LEA R2,1[R0] LOAD R3,z[R0] ADD R4,R1,R2 ADD R4,R4,R3 STORE R4,x[R0]
; R1 = 50 ;x=50
;R1=2 ;R2=z ;R3=2*z ;y=2*z
;R1=x ;R2=1 ;R3=z ;R4=x+1 ; R4 =x+1+z ;x=x+1+z
x = x+1+z;
Systems and Networks 5. Assembly Language Programming 9

Example: Register Variable Style
; Usage of register variables: ; R1=x
LEA R1,50[R0] LOAD R3,z[R0] LEA R4,2[R0] MUL R2,R4,R3 LEA R4,1[R0] ADD R1,R1,R4 ADD R1,R1,R3 STORE R1,x[R0] STORE R2,y[R0]
; x = 50 ;Getz ;R4=2 ; y = 2*z ;R4=1 ;x=x+1 ;x=x+z ;Savex ;Savey
x = x+1+z;
Systems and Networks 5. Assembly Language Programming 10

An Example Program: Add
; Program Add. y = x+32; initially x = 10
; The program
LOAD R1,x[R0]
LEA R2,32[R0]
ADD R3,R1,R2
STORE R3,y[R0]
; R3 = x+32
; y = x+32
; The data
10 ; 10 0 ; 0
Each line of source will produce one or two words of object code. Some words are instructions, others are data.
Use the Sigma 16 environment to generate an assembler listing for this program
Systems and Networks 5. Assembly Language Programming 11

Assembler Listing
When you run the assembler, and give it program Add as input, it will print a listing like this…
Line Address Machine code Program
0 1 2 3 4 5 6 7 8 9
; Program Add
; y = x+32; initially x = 10
LOAD R1,x[R0] ; R1 = x
LEA R2,32[R0] ; R2 = 32
ADD R3,R1,R2
STORE R3,y[R0]
TRAP R0,R0,R0
; R3 = x+32 ; y = x+32 ; Stop
Location of word Object code Source code
Systems and Networks 5. Assembly Language Programming 12

Practice exercise
• Translate this code to assembly language:
z = x – (y * 3);
• Now try the code out in Sigma16 (after watching the Using Sigma16 tutorial) you will need to add a TRAP statement at the end and data statements to define x, y, z. It is also good practice to add comments as suggested in the tutorial.
Note that the values you set in the DATA statements for x and z do not matter, because the code sets x, and z (which is a result) is overwritten anyway. But you can try different values of y and these will produce different results in z (you will need to re-assemble each time though: getting data into a Sigma16 program is admittedly clumsy).
Locate the address the assembler has allocated to z and use a memory window to see your results (in hex). Convince yourself the code is producing the right results!
Systems and Networks 5. Assembly Language Programming 13

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com