TK2123-Lecture13-Assembly Language Level

Download Report

Transcript TK2123-Lecture13-Assembly Language Level

TK 2123
Lecture 13: Assembly Language
Level (Level 4)
Ass. Prof. Dr Masri Ayob
Why Use Assembly Language?

Assembly language programming is difficult.



Writing a program in assembly language takes much
longer than writing the same program in a high-level
language.
Much longer to debug and much harder to maintain.
Why use assembly language?


4 April 2016
Performance
Access to the machine
Prepared by: Dr Masri Ayob
2
Why Use Assembly Language?

Performance


An expert assembly language programmer can often
produce code that is much smaller and faster than a highlevel language programmer can.
 Some application – speed and size are critical. E.g.
embedded application (code on a smart card,
handphone, etc.)
Access to the machine.

4 April 2016
Some procedure need complete access to the hardware.
 E.g. low-level interrupt, trap handlers in OS, device
controller, etc.
 This is usually impossible in high-level language.
Prepared by: Dr Masri Ayob
3
Why Use Assembly Language?
Comparison of assembly language and high-level language
programming, with and without tuning.
4 April 2016
Prepared by: Dr Masri Ayob
4
Assembly Language


Assembly language:
 Assembly language is used for most programming
because it is extremely difficult to program a
microprocessor in its native, that is hexadecimal machine
language.
Assembler:
 An assembler is a program that converts software written
in symbolic machine language (the source program) into
hexadecimal machine language (object program).
 The primary reason to use assembler is because
development and modification are always difficult in
machine language.
4 April 2016
Prepared by: Dr Masri Ayob
5
Assembly Language


Instruction is a command to the microprocessor to
perform a given task on specified data.
Each instruction has two parts:


4 April 2016
one is the task to be performed, called the operation code
(op-code),
the second is the data to be operated on, called the
operand.
 The operand (or data) can be specified in various ways.
 It may include an internal register or a memory
location.
 In some instructions, the operand is implicit.
Prepared by: Dr Masri Ayob
6
Assembly Language

4 April 2016
The Two-pass Assembler :
 Read programme two times.
1. Generate a table of the labels/symbols
within the source program.
2. Develop hexadecimal version of the source
program.
 Allow forward addressing (the software can
jump ahead to an instruction in a program).
Prepared by: Dr Masri Ayob
7
Assembly Language
4 April 2016
Prepared by: Dr Masri Ayob
8
Assembly Language

The assembler always assumes that the first instruction of the
program is stored at memory address 0000H unless otherwise
directed by the ORG command.
4 April 2016
Prepared by: Dr Masri Ayob
9
Assembly Language

Pass One:





4 April 2016
The assembler scans the source program during the
first pass and generates a table of the labels found
within the source program.
Each entry in the label table contains the label and
the address where the label appears in the program.
During the first pass the assembler determines the
length of each instruction by updating an internal
program counter.
This internal program counter allows the assembler
to complete the label table by equating each label
with the counter.
Once the label table is complete the second pass
begin.
Prepared by: Dr Masri Ayob
10
Assembly Language

Pass Two:
 During the second pass of the source program,
the assembler forms the object program.
 This occurs by referring to the label table for any
labels that appear in the program and to an
instruction table.
 The instruction table contains all the opcodes in
both symbolic and machine language forms.
 The tables help convert the source program into
the object program.
4 April 2016
Prepared by: Dr Masri Ayob
11
Assembly Language


4 April 2016
Assembly Language Statement:

Format :
Label Field.

Contains a symbolic memory address that refers to the
statement in a program. Labels are optional.

Labels are constructed from alphanumeric characters
and must begin with any letter of the alphabet.
Prepared by: Dr Masri Ayob
12
Format of an Assembly Language Statement (1)
Computation of N = I + J. (a) Pentium 4.
4 April 2016
Prepared by: Dr Masri Ayob
13
Format of an Assembly Language Statement (2)
Computation of N = I + J.
4 April 2016
(b) Motorola 680x0.
Prepared by: Dr Masri Ayob
14
Format of an Assembly Language Statement (3)
Computation of N = I + J. (c) SPARC.
4 April 2016
Prepared by: Dr Masri Ayob
15
Assembly Language

Opcode field:


Operand field:




4 April 2016
This field must contain opcodes.
May contain register name, data or labels.
If more than one of these is present, they must be separated with
comma.
Data must be encoded as decimal, binary, octal, hexadecimal, or
ASCII.
ASCII must appear as one of more letters surrounded by apostrophe.
Prepared by: Dr Masri Ayob
16
Assembly Language

4 April 2016
Operand arithmetic operations.
Prepared by: Dr Masri Ayob
17
Assembly Language

4 April 2016
Comment field.

Must begin with semicolon in most 8085 assemblers
and may continue to the end of the line only.

Use asterisk * or semicolon ; if the comment should
continue into the next line.

Example :
Prepared by: Dr Masri Ayob
18
Pseudoinstructions

Assembler pseudo operations.
 Directives to the assembler program that may or
may not generate machine code.
 Examples :
 END, DB, DW, DS, ORG, EQU, IF, ENDIF, SET,
GLB, EXT, TITLE, SPC.
 All pseudo operations must appear in the opcode
field of a statement.
4 April 2016
Prepared by: Dr Masri Ayob
19
Pseudoinstructions

Define Byte (DB).
 Defines 8-bit memory data for a program.
 Multiple one byte data, comma ( , ) as a
separator.
4 April 2016
Prepared by: Dr Masri Ayob
20
Assembly Language : Example
4 April 2016
Prepared by: Dr Masri Ayob
21
Pseudoinstructions

Origin (ORG).
 Changes the starting location of the
program to another address besides
0000H.
 Can be used at any place in a program to
change the location of the assembled
machine language instructions or data.
4 April 2016
Prepared by: Dr Masri Ayob
22
Assembly Language : Example
4 April 2016
Prepared by: Dr Masri Ayob
23
Pseudoinstructions

4 April 2016
Define Word (DW).
 Pseudo operation stores a 16-bit number
in the memory for use by a program.
 Defines no only numeric data but also
memory addresses and label.
Prepared by: Dr Masri Ayob
24
Assembly Language : Example
4 April 2016
Prepared by: Dr Masri Ayob
25
Pseudoinstructions

4 April 2016
Define Storage (DS).
 Reserves space in a program for
variable data.
 Does not place any specific data into
the reserved area of memory.
Prepared by: Dr Masri Ayob
26
Assembly Language : Example
4 April 2016
Prepared by: Dr Masri Ayob
27
Pseudoinstructions

4 April 2016
Equate (Equ).
 Equates a label to another label or
value.
 Note that the EQU statement label does
not contain a colon ( : ).
Prepared by: Dr Masri Ayob
28
Assembly Language
4 April 2016
Prepared by: Dr Masri Ayob
29
Macro


Assembly language programmer frequently need to
repeat sequence of instructions several times within a
program.
Solution:


4 April 2016
Use subroutine (CALL instruction).

Disadvantage: Procedure CALL overhead.
Use Macro

Easy and efficient solution

Is a way to give name to a piece of text.
Prepared by: Dr Masri Ayob
30
Macro Definition, Call, Expansion
(1)
Macro
Assembly language code for interchanging P and Q twice.
(a) Without a macro.
(b) With a macro.
4 April 2016
Prepared by: Dr Masri Ayob
31
Macro Definition, Call, Expansion
(2)
Comparison of macro calls with procedure calls.
4 April 2016
Prepared by: Dr Masri Ayob
32
Macros with Parameters
Nearly identical sequences of statements.
(a) Without a macro. (b) With a macro.
4 April 2016
Prepared by: Dr Masri Ayob
33
Thank you
Q&A
4 April 2016
Prepared by: Dr Masri Ayob
34