+ All Categories
Home > Documents > Week 3. Assembly Language Programming Difficult when starting assembly programming Have to work at...

Week 3. Assembly Language Programming Difficult when starting assembly programming Have to work at...

Date post: 14-Dec-2015
Category:
Upload: addison-boucher
View: 217 times
Download: 0 times
Share this document with a friend
Popular Tags:
46
Week 3
Transcript

Week 3

Assembly Language Programming

Assembly Language Programming

Difficult when starting assembly programmingHave to work at low level

Use processor instructions> Requires detailed understanding of instructions

Manage memory and register resourcesBenefits are significant

Better understanding of how high level languages (HLL) workKnowledge of limitations of the hardwareSometimes, programmer must resort to assembly language

Issues of speed and code density However, today’s HLL compilers are very powerful and can generate highly

optimised code

Using Assembly LanguageUsing Assembly Language

Editor AssemblerLinker (opt)

SourceCode

ObjectCode

ExecutableProgram

test.a51 test.obj test.hextest.lst

Using Assembly LanguageUsing Assembly Language What happens after creating executable file?

Must be verified and tested!! How is this done?

There are 2 basic strategies1. Use real hardware to run and test code2. Use simulation software to simulate the operation of a real hardware

- This is the approach that we will take

Editor Assembler Linker

Edit

Assembler

Linker

Run

No assemblyerrors

No run-time/logical errors

No linking errors

Run-time and/orlogical errors

Link-time errors

Assembly errors

Testing and

Debugging

Using Assembly Language

Using Assembly Language

Arithmetic InstructionsArithmetic InstructionsThe arithmetic instructions provide instructions for

performing the basic arithmetic operations of addition, subtraction, multiplication and division on integer operands

For most of the arithmetic instructions, the accumulator register acts as an operand and the location of the result

E.g. ADD A,#23

All the addressing modes previously encountered can be used

A 23

ALU

Binary AdditionBinary Addition

Adding binary values works the same as adding decimal numbers, except now the base is different, i.e. base 2 instead of 10

ExampleAdd these 2 four bit numbers

1 0 1 1 1 0 0 11 0 1 0 0

111111 Carry bits

Arithmetic instructions: ADD

Arithmetic instructions: ADD

The add instruction allows two 8-bit numbers to be added. As an example, lets consider the instruction ADD A,#23 and lets assume that A contains the value 31 beforehand.

The outcome of the addition is as follows

(23) 00010111(31) 00011111

(54) 000110110Note that the result is nine

bits, but our registers are only 8 bits.

The extra bit is called the carry bit and it is stored in a special register called the program status word register (PSW)

Arithmetic instructions: ADD

Arithmetic instructions: ADD

Another example(215) 11010111(127) 01111111(342) 101010110

The value stored in A is 01010110 or decimal 86 and the carry bit is a 1

The examples considered so far assume unsigned numbers, I.e. no negative numbers

Since an 8 bit unsigned number has the largest value of 255, the largest possible sum is 510, which can be represented by 9 bits

I.e. the 8 bit result in A and the carry bit

So everything is great, or is it?????

Number RepresentationsNumber Representations

Unsigned number

10010110

Has decimal value 150

Signed (2’s complement) number10010110

Has decimal value –106

How can this be????

ANS: The MSB of a signed number (using 2’s complement) has a negative weighting

Number RepresentationsNumber Representations

UnSignedBit weightings

128 64 32 16 8 4 2 1

SignedBit weightings

-128 64 32 16 8 4 2 1

Example1010010

represents unsigned 160(128+32+2)Represents signed -96(-128+32+2)

Arithmetic instructions: ADD

Arithmetic instructions: ADD

When dealing with 2’s complement values, can all results of additions be represented correctly?

The answer is NO, but there is a way to check if the result is not correct

There is an overflow (OV) bit in the PSW register which is set when the result of an arithmetic result is out of the allowable range for 2’s complement numbers

See later how to access the OV bit with instructions

Well, normally signed numbers are needed and a 2’s complement representation is used.

When using 8 bits, a 2’s complement representation allows values from –128 to +127 to represented.

Consider an example

Lets add 43 and –68(43) 00101011(-68) 11000001(-25) 011100111

The addition operation is carried out the same as before

What is important here is What is important here is our (the programmer’s ) our (the programmer’s ) intrepretation of the bitsintrepretation of the bits

Arithmetic instructions: ADD

Arithmetic instructions: ADD

Arithmetic instructions: ADD

Arithmetic instructions: ADD

ExampleAdd the contents of R1 to

A and store result in R2

ADD A,R1

The carry (C) and the overflow (OV) bits are affected by the ADD instruction

R1 C

A

OV

015

23

Before Add

0

38

After Add

Example 1Example 1

Consider the addition of the following binary values

01100010 +01000111

R1

A

01100010

01000111

Before Add10101001

10101001

After Add

ADD A,R1

Example 1 (Unsigned interpretation)

Example 1 (Unsigned interpretation)

This example illustrates dealing with unsigned numbers

OV bit has no meaning in unsigned case

R1 C

A

OV

98

71

Before Add

0

1

169

After Add

Consider the addition of the following binary values

01100010 +01000111

10101001

Example 1 (signed interpretation)

Example 1 (signed interpretation)

This example illustrates dealing with signed numbers

C bit has no meaning in signed case

R1 C

A

OV

98

71

Before Add

0

1

-87

After Add

Consider the addition of the following binary values

01100010 +01000111

10101001

Lecture 2

Example 2Example 2

Consider the addition of the following binary values

11101110 +00010111

R1

A

11101110

00010111

Before Add100000101

00000101

After Add

ADD A,R1

Example 2 (Unsigned interpretation)

Example 2 (Unsigned interpretation)

This example illustrates dealing with unsigned numbers

OV bit has no meaning in unsigned case

R1 C

A

OV

238

23

Before Add

1

0

5

After Add

Consider the addition of the following binary values

11101110 +00010111

100000101

Example 2 (Signed interpretation)

Example 2 (Signed interpretation)

This example illustrates dealing with signed numbers

C bit has no meaning in signed case

R1 C

A

OV

-18

23

Before Add

1

0

5

After Add

Consider the addition of the following binary values

11101110 +00010111

100000101

Arithmetic instructions: ADD

Arithmetic instructions: ADD

The allowable instructions for addition are

ADD A,sourceADD A,#data

Where source can be any of Rn, direct or @Ri

Example programExample program

Consider a program to add the contents of the registers R4, R5 and R6 with the value in the address location 50H. Store the result in R7

MOV A,R4 ; A = R4ADD A,R5 ; A = A + R5ADD A,R6 ; A = A + R6ADD A,50H ; A = A + contents of 50HMOV R7,A ; Move result of adds into R7

Example ProgramExample Program

The above program will only work properly if the sum is within the range –128 to 127

To test the program, it is necessary to initialise the registers and memory location with test values

MOV R4,#-7MOV R5,#-37MOV R6,#15MOV 50H,#32

Testing OV or C bit after the last addition only indicates if the last addition overflowed!

Arithmetic instructions: ADDC

Arithmetic instructions: ADDC

A variation on the ADD instruction is the ADDC instruction.

This is the known as the ADD with carry instructionExample

ADDC A,#34Here the value of the accumulator is added to 34 and the carry

bit is then added, with the result stored in the accumulator.This instruction is particularly useful when performing 16

bit or 32 bit additions using 8 bit values

Arithmetic instructions: ADDC

Arithmetic instructions: ADDC

Example:Registers R1 and R0 together store a 16 bit signed integer.

Register R3 and R2 also store a 16 bit signed integer. Add the two 16 bit numbers and store the result in R5 and R4.MOV A,R0ADD A,R2MOV R4,AMOV A,R1ADDC A,R3MOV R5,A

R1

R5 R4

R3 R2

R0

SubtractionSubtraction

Again, same idea as subtracting decimal numbersJust as a carry is used when doing an addition a borrow

is used for subtraction. Example

Subtract these 2 four bit numbers 1 0 0 1 0 1 1 10 0 0 1 0

1111 Borrow bits

Arithmetic instructions: SUBB

Arithmetic instructions: SUBB

Performs a subtraction operation with a borrow into the least significant bit (LSB)

The borrow is actually in the carry bit in the PSWIf no borrow in used, then make sure to clear the carry

bitTo do this, use the following instruction

CLR CThis instruction uses the accumulator as a source

operand and the destinationThe OV bit and carry bit are affected by this instruction

Arithmetic instructions: SUBB

Arithmetic instructions: SUBB

ExampleSubtract the value in R1 from

R0 and store result in R2, i.e. R2=R0-R1

CLR CMOV A,R0SUBB A,R1MOV R2,A

R1 C

R2

R0

A

OV

0

8

15

23

23

0

Note C and OV bits before and after SUBB

Summary for arithmetic instructions

Summary for arithmetic instructions

The accumulator acts as an operand and destination for most arithmetic operands

Certain bits in the PSW are modified following arithmetic instructions

Carry (C) bitOverflow (OV) bit

Only has meaning when numbers are treated as signed numbersNeed to know what the data represents, so as to interpret results

correctlyUnsignedSigned (2’s complement)Other (e.g. BCD)

Appendix Week3

Programming LanguagesProgramming Languages

The first programmers hardwired circuits together and used punch cards to create a program

High level programming languages helps bridge the natural communication gap between man and machine.

Programmer is not concerned with the low level operation of computer hardware.

Can think and develop programs at an abstract level. Because people like to express themselves in terms of

natural languages like English, the use of high level languages make it easier to develop programs.

Types of Programming Languages

Types of Programming Languages

Machine Code

01001110101000111010101101010101010101000101110

Types of Programming Languages

Types of Programming Languages

Assembly Language

MOV A,R0ADD A,R2MOV R4,AMOV A,R1ADDC A,R3MOV R5,A

Types of Programming Languages

High Level LanguageLarge number of different high level languages

if goals_teamA > goals_teamB then

writeln(‘Team A won the match’')

else

if goals_teamA = goals_teamB then

writeln(‘Match was a draw’);

else

writeln(‘Team B won the match’);

Getting started with a high level programming

language

Getting started with a high level programming

languageCan write our code in a high level language, but the

computer system can only understand machine code.Need to convert from a high level programming

language to machine code in an unambiguous manner.

Program in high level language ===> Machine code

To begin to see how we can accomplish this, first need to create the high level program.

Text EditorText Editor

An editor or text editor is an application program.There are many different editors available.

Using an editor the computer acts like a typewriter. The user is able to enter or edit any text and save the text to disk.

High level program files (source code) can be created/edited using an editor.

Example

TranslatorsTranslators

A high level program (source code) needs to be translated to a machine readable format, before it can be executed (run) on a computer.

Machine readable code is usually referred to as object code.

A compiler is used to perform this process.

ProgrammingProgrammingProgrammer needs to learn to be patientProper design is key

System analysis and designLook at ideas on problem solving in week3

Good demand for programmersThere are always problems there to be solved!

Unsigned numbersUnsigned numbers

An 8 bit unsiged number has a value determined as follows

Where is 1 or 0 and represents the binary bitsFor example, given the value 10010110, then its

value is

7

0

2i

i

ia

ia

01

234567

2021

212021202021

15002401600128

Signed numbersSigned numbers

An 8 bit unsiged (2’s complement) number has a value determined as follows

Where is 1 or 0 and represents the binary bitsFor example, given the value 10010110, then its

value is

6

0

7

722

i

i

iaa

ia

0

1234567

20

21212021202021

10602401600128

Exercises

ExercisesExercises

1. What is the process in going from coding an assembly language program to executable machine code?

2. What is the ALU?3. Perform the binary addition of the following 2 bytes.

Give the result in binary, decimal and hexadecimal1. 101110112. 10000110

4. How are signed numbers represented in binary?5. Does the ADD instruction need to know whether signed

or unsigned numbers are being added?

ExercisesExercises

6. When is the carry bit used to indicate overflow?7. When is the OV bit used to indicate overflow?8. Given the following 8 bit value

6. 100010017. What is its value if

6. Its interpreted as unsigned7. Its interpreted as signed

9. Detail the code to add the contents of R0 to R5 and store the result in R0.

ExercisesExercises

10. What does the ADDC instruction do? Give an example use.

11. Detail how the following 2 binary values are subtracted1000101101111010

12. What is the SUBB instruction used for?13. If a borrow out from the MSB is generated when using

SUBB, where is it stored.


Recommended