Title: Assembly Language
1Assembly Language
- Chapter 5
- CIS-2261
- Foundations in Computer Science
2Homework Chapter 5
Exercises 1, 2, 3, 4, 6, 8, 9, 11, 12, 14, 17,
18, 24, 25
3OVERVIEW
- Assemblers
- Decimal I/O and Immediate Addressing
- Symbols
- Assignment Statements
4Introduction
- The Assembly level was invented to relieve the
programmer of the tedium of programming in binary - We just covered the operation of a computer at
level 3 - This chapter focuses on how level 5 abstracts the
details at level 3
5Assemblers
- Two types of bit patterns at level 3
- Bit pattern for instructions
- Bit pattern for data
- The types are a direct consequence of the von
Neumann design - Data and program share same memory with a binary
representation for each
6Assemblers
- Assembly language contains two types of
statements corresponding to each bit pattern - Mnemonic statements correspond to the
instruction bit patterns - Pseudo-operations correspond to the data bit
patterns
7Instruction Mnemonics
Level 3 0000 1000 0000 0000 1001 1010
Opcode
Reg Spec
Address mode
Level 5 LOADA h009A, i
Instruction Meaning Load accumulator with the
contents of the Operand Specifier (h009A)
8Instruction Mnemonics
Level 3 0000 1001 0000 0000 1001 1010
Opcode
Reg Spec
Address mode
Level 5 LOADA h009A, d
Instruction Meaning Load accumulator with the
contents from memory pointed to by the Operand
Specifier (A lt memh009A)
9Instruction Mnemonics
Level 3 0000 1011
Opcode
Reg Spec
Address mode
Level 5 LOADA ,X
Instruction Meaning Load accumulator with the
contents from memory pointed to by the Base
Index Register (memBX)
10Examples of LOADR
Machine Language
Assembly Language
0000 1000 0000 0000 1001 1010 LOADA h009A,
i 0000 1001 0000 0000 1001 1010 LOADA h009A,
d 0000 1010 0000 0000 1001 1010 LOADA h009A,
s 0000 1011 LOADA , x 0000 1100 0000 0000
1001 1010 LOADX h009A, i 0000 1101 0000 0000
1001 1010 LOADX h009A, d 0000 1110 0000 0000
1001 1010 LOADX h009A, s 0000 1111 LOADX ,
i
11Pep/6 Instruction Set
12Pep/6 Instruction Set
13Pep/6 Instruction Set
14Pep/6 Instruction Set
15Pep/6 Instruction Set
- DECI, DECO, HEXO are new instructions available
only at level 5 - Instructions are implemented by the operating
system
16Pseudo-Operations
- Pseudo-ops are assembly language statements
- Do not have opcodes
- Do not correspond to any of the 32 instructions
- Pep/6 has 8 pseudo-ops
17Pseudo-Operations
18Program 5.1
Assembler Input CHARO h0007, d Output
H located at mem0007 CHARO h0008, d
Output i located at mem0008 STOP .ASCII
/Hi/ Generate contiguous bytes
of ASCII .END Assembler Output E1 00 07 E1 00 08
00 48 69 zz Program Output Hi
19Assemblers
- An assembler is a program that translates
assembly level (level 5) code to machine level
code (level 3) - Developed to relieve the early programmer of the
need to hand assemble his/her program - Assembler input is an assembly program (source
code) and output is that program translated to
machine level (object code) - Assembler does not execute the program just
gets it ready for the loader to load
Input
Output
Processing
CHARO h0007, d CHARO h0008, d STOP
.ASCII /Hi/ .END
E1 00 07 E1 00 08 00 48 69 zz
Assembler
Object code
Source code
20Program 5.2
Assembler Input CHARI h000D, d
Input first character CHARI h000E, d
Input second character CHARO h000E, d
Output second character CHARO h000D, d
Output first character STOP .BLOCK
d1 Storage for first character .BLOCK
d1 Storage for second character .END Assembler
Output D9 00 0D D9 00 0E E1 00 0E E1 00 0D 00 00
00 zz Program Input Hi Program Output Hi
.BLOCK - generate a block of bytes
d denotes decimal character expected
21Program 5.3
.WORD - assembler generates one word instead of
d bytes AND the programmer can specify the
content of the word
22Assembler
- .WORD and .BYTE pseudo-op similar to .BLOCK
except - .WORD - assembler generates one word instead of
d bytes AND the programmer can specify the
content of the word - .BYTE - assembler generates one byte instead of
d bytes AND the programmer can specify the
content of the byte
23Using the Pep/6 Assembler
- First the assembler is loaded into main memory
and the applications program is taken as the
input file - The output of this run is the machine language
version of the applications program - This machine language is then loaded for running
24Using the Pep/6 Assembler
Input
Output
Processing
CHARI h000D, d CHARI h000E, d CHARO
h000E, d CHARO h000D, d STOP .BLOCK
d1 .BLOCK d1 .END
Assembler (machine language)
D9 00 0D D9 00 0E E1 00 0E E1 00 0D 00 00 00 zz
FIRST RUN
D9 00 0D D9 00 0E E1 00 0E E1 00 0D 00 00 00
zz
up
pu
SECOND RUN
25General Rules for Pep/6 Assembler
- Place at least one space after the mnemonic or
dot command - Use any combination of upper and lower case
letters - The assembler will protest if you made any errors
and will not generate any object code - Can generate an assembler listing if desired
26Program 5.2 - Source Code
27Program 5.2 - Assembler Listing
28Cross Assemblers
- An assembler that produces the object code
program for a different machine than the one
running the assembler - Brand X producing the object code for download to
Brand Y - Brand X is the Host machine
- Brand Y is the Target machine
- Often used if programming small special purpose
machines
29OVERVIEW
- Assemblers
- Decimal I/O and Immediate Addressing
- Symbols
- Assignment Statements
30Immediate Addressing
- Direct addressing uses the Operand Specifier as
the address in main memory to find the operand - Oprnd MemOprndSpec
- Immediate addressing uses the Operand Specifier
to actually be the Operand - Oprnd OprndSpec
31Program 5.1 - Direct Addressing
32Program 5.1 Assembler Listing
33Program 5.4 - Immediate Addressing
34Program 5.4 - Assembler Listing
c stands for a character constant
Assembler translates CHARO c/H/, i as E00048
1110 0000 0000 0000 0100 1000
35Decimal I/O
- Still need to find a better way of doing decimal
I/O - Right now pretty hard to do
- One character at a time
- Need to convert
- New instructions for handling decimals!
- DECI
- DECO
36Decimal I/O - Program 5.5
DECI and DECO are really handled by the operating
system
37Decimal I/O
- Previous program placed all data at the top of
the program - First instruction is an unconditional branch
around the data - Can now change code without having to refigure
everything! - Notice the operation of the PC the von Neumann
execution cycle (fetch, increment, execute,
repeat)
38Decimal I/O
- Operating System provides the DECI and DECO
instructions - DECI converts a sequence of ASCII characters to a
single Word in twos complement - DECO does just the opposite - twos complement
word to sequence of ASCII - RANGE??
39Hexadecimal Output
- Third instruction implemented by the operating
system - No Hex input available
- HEXO outputs one Word of information as four
hexadecimal digits
40HEXO - Program 5.6
41Disassemblers
- Assemblers use one-to-one mapping to transform an
assembly program into a machine program - Translation process is unique and not invertable
- Disassembler tries to recover the source program
from the object program - not 100 successful
- Prone to ambiguity errors
42OVERVIEW
- Assemblers
- Decimal I/O and Immediate Addressing
- Symbols
- Assignment Statements
43Symbols
- Talked about branching around data at the
beginning of a program - Still difficult process because you have to count
in hexadecimal - If you modify your data section, you have to go
back in and modify all of the instructions that
reference the changed data section - ughhh!
44Symbols
- We need to develop a way for the assembler to
associate a memory address with data - USE SYMBOLS
- Use symbols within the assembly code to reference
the data instead of using the actual address - Assembler now needs a look-up table to reference
a symbol to a specific location in memory - Build the symbol table during the assembly process
45Symbols
- Use syntax rules for symbols that are similar to
C - First character is a letter
- Following characters must be letters or digits
- Maximum length is 8
- Terminate a symbol by using a colon
- Place the symbol at the beginning of the line it
references - No spaces
Defines the symbol num and allocates two bytes
for it
num .BLOCK d2
46Symbols
- When the assembler detects a symbol
- Store the symbol and its value in a symbol table
- The value is actually the memory address of the
symbol - When referring to the symbol in the code, do not
include the colon
47Program 5.7
48Symbols
- Remember - the value of a symbol is an address,
not the content of the cell at that address - The value of the cell will not normally be known
until runtime unless the symbol is a constant - The symbol is reserving space for the runtime
variable - Also - symbols are easier to read and understand
than straight hex code
49Program 5.8
.EQUATE acts like a constant definition
50OVERVIEW
- Assemblers
- Decimal I/O and Immediate Addressing
- Symbols
- Assignment Statements
51Compilers
- Compiler translates high-order language programs
into lower level programs - Some compilers translate directly to machine
language - other compilers translate to assembly and require
an assembler to translate to machine - A compiler is a program like any other program
- Must execute as machine level code
- Input to compiler is called the source program
- Output of the compiler is called the object
program
52Compilers
- Mappings from high-order to assembly is not
typically one-to-one - Normally the mapping will be one-to-many
- Not all high-order elements are assembled
- include - used to tell the compiler how to make
the correct interface to the operating system
53Compilers
- Its time to start looking at how a compiler would
translate high-order language into lower level
languages!! - Lets start simple - how to translate an output
statement
54Program 5.10
High -Order Language (Source Code) include
ltiostream.hgt main() cout ltlt Love
This is essentially a single statement program.
55Program 5.10 Object Code
Translation of a simple single C statement This
is not the only possible translation!!
56Program 5.10 Machine Listing
57Compilers
Input
Output
Processing
include ltiostream.hgt main() cout ltlt
Love
E0 00 4C E0 00 6F E0 00 76 E0 00 65 00 zz
Compiler
Level 6 - Level 3
Machine code
Source code
CHARO c/L/, i CHARO c/o/, i CHARO
c/v/, i CHARO c/e/, i STOP
.END
include ltiostream.hgt main() cout ltlt
Love
Compiler
Level 6 - Level 5
Source code
Assembly code
58Variables and Types
- Every C variable has three attributes
- name
- type
- value
- Compiler reserves memory cells in the machine
language program for each and C variable - A variable in C is simply a memory cell in
machine/assembly code
59Variables and Types
- Level 6 programs refer to variables by names
which are C identifiers - Level 3 programs refer to them by addresses
- Value of the variable is the value in the memory
cell at the address associated with the C
identifier - Compiler must remember the which address
corresponds to which C variable name - Use a symbol table very similar to that used by
an assembler
60Variables and Types
- Compiler symbol table more complicated than table
used by an assembler - More than 8 characters
- Must store variable type as well as associated
address
61Program 5.11 - Assignment Statements
High -Order Language (Source Code) include
ltiostream.hgt char ch int i main() cin gtgt ch
gtgt i i 5 ch
cout ltlt ch ltlt endl ltlt i
62Program 5.11 Assembly List
char ch
int i
Endl h000A implied
63Program 5.11 - Compiler Symbol Table
64Code Generation in Program 5.11
Compiler Translates To
Cin gtgt ch gtgt I
Chari h0003, d Deci h0004, d
LoadA I, d AddA d5, i StoreA I, d
I 5
LdbytA ch, d AddA d1, i StbytA ch, d
Ch
65Type Compatibility
- Compilers also need to keep track of different
variable types and not allow certain operations
to be performed on them if the type is incorrect - example should never be able to take modulus of
a floating point number - Assembly programming would allow this to happen
even though the result would be garbage
66Program 5.12 - Source Code
High -Order Language (Source Code) include
ltiostream.hgt const int bonus 5 int
exam1 int exam2 int exam3 main() cin gtgt
exam1gtgt exam2 score (exam1
exam2)/2 bonus cout ltlt score
ltlt score
67Program 5.12 - Assembly Listing
68Program 5.12 - Symbol Table
Note that there is no assembly code for bonus.
You dont need to reserve memory space for a
constant. Instead put it in the table and store
its value directly at the using location. That is
how .EQUATE is handled by the compiler.
69END Chapter 5