Intro to Assembly Programming

Download Report

Transcript Intro to Assembly Programming

Assembly Language
Programming
CS208
Assembly Language

Assembly language allows us to use convenient
abbreviations (called mnemonics) for machine
language operations and memory locations.

Each assembly language is specific to a particular
hardware architecture, and can only be used on a
machine of that architecture.

An assembly language program must be translated
into machine code before it can be executed. The
program that tells the computer how to perform the
translation is called an assembler.
Assembly Language

When a processor chip is designed, it is
designed to understand and execute a set of
machine code instructions (OpCodes) unique to
that chip.

One step up from machine code is assembly
code. Each machine code instruction is given a
mnemonic (name), so that it is easier for human
beings to write code.

There is generally a one-to-one correspondence
between the assembly languages mnemonic
instructions and the machine language numeric
instructions.
Model Assembly Instructions

Our assembly language instructions have two
parts:
– The operation code specifies the operation the
computer is to carry out (add, compare, etc)
– An address that allows the instruction to refer to a
location in main memory

The CPU runs each instruction in the
program, starting with instruction 0, using the
fetch-decode-execute cycle.
Review of the
Fetch-Decode-Execute Cycle

The CPU fetches the next instruction from the
address contained in the Program Counter
and places the instruction in the Instruction
Register.
– When a program starts, the program counter
contains 0, so the instruction at address 0 is
fetched.

Immediately after the instruction fetch, the
CPU adds 1 word to the contents of the
Program Counter, so that it will contain the
address of the next sequential instruction.
Review of the
Fetch-Decode-Execute Cycle

The CPU decodes the instruction in the
Instruction Register and determines what
operations need to be done and what the
address is of any operand that will be used.

The specified operation is executed (add,
compare, etc).

After execution of the instruction has been
completed the cycle starts all over again
(unless the instruction terminates the program).
CPU
CPU Registers

The Instruction Register (IR) contains the
actual instruction which is currently being
executed by the CPU.

The Status Register records the result of
comparing the contents of register A with the
contents of register B.

The Program Counter (PC) contains the
address of the next instruction to be
executed by the program.
CPU Registers

Registers A & B hold the operands for
each arithmetic operation (ie. the values
on which the operation will be performed).
After the operation has been carried out,
the result is always stored in Register B.

Therefore, after an arithmetic operation
has been performed, the second operand
is no longer stored in Register B, because
it has been overwritten by the result of the
operation.
CPU Registers

After a comparison has been done, the
Status Register will hold a code that stores
the results of the comparison.

The results are coded as follows:
-1
0
1
if (A < B)
if (A = B)
if (A > B)
Model Assembly Language
Instructions
Operation
STP
LDA
LDB
STR
INP
PNT
What it means to the CPU
Stop the program
Load register A with value from a
specified memory location
Load register B with value from a
specified memory location
Store register B value to a specified
memory location
Store data input by user to a specified
memory location
Print the value stored in a specified
memory location to the screen
Model Assembly Language
Instructions
Operation
What it means to the CPU
JLT
Jump if less than (Status register = -1)
to a specified memory location
JGT
Jump if greater than (Status register = 1)
to a specified memory location
JEQ
Jump if equal (Status register = 0)
to a specified memory location
JMP
Unconditional jump to a specified
memory location
CMP
Compare register A to register B and
set Status Register value
Model Assembly Language
Instructions
Operation
What it means to the CPU
ADD
Add (register A + register B) and
store sum in register B
SUB
Subtract (register A - register B) and
store difference in register B
MUL
Multiply (register A * register B) and
store product in register B
DIV
Divide for quotient (register A/register B)
and store quotient in register B
MOD
Divide for remainder (register A/register B)
and store remainder in register B
Steps to write Assembly Programs

Create C++ Program (only the statements
between the { and } brackets are needed)

Translate each C++ statement to the
equivalent assembly statement(s)

Number the assembly language program
starting from 0

Replace memory names by memory address
numbers of empty memory cell

Resolve jumps (replace with number of
memory cell jumping to)
C++ to Assembly Language
Statement
Assembly equivalent
#include
none
void main()
none
const
value in memory cell
int, double, char
address of memory cell
cin
INP
cout
PNT
assignment (=)
val3 = val1 + val2
LDA Val1
LDB Val2
ADD
STR Val3
}
STP
Sample Program #1
Program #1.
Write an assembly language program
that will get a number as input from
the user, and output its square to the
user.
Sample Program #1
Step 1:
Write an algorithm to describe the steps needed
to solve our problem.
Algorithm:
1. Input a number and store it in memory.
2. Compute the square by multiplying
the number times itself.
3. Output the results.
Sample Program #1
Step 2: Write the C++ code
{
int Number , Square;
cout << "Enter a number: ";
cin >> Number;
Square = Number * Number ;
cout << Square;
}
Sample Program #1
Step 3: Translate C++ code to assembly
{
cout << "Enter a number: ";
cin >> Number;
INP number
square = number * number;
LDA number
LDB number
MUL
STR square
cout << Square;
PNT square
}
STP
Sample Program #1
Step 4: Number assembly code lines starting
from 0
0
1
2
3
4
5
6
INP
LDA
LDB
MUL
STR
PNT
STP
number
number
number
square
square
Sample Program #1
Step 5: Replace memory names with
empty memory locations after STP
0
1
2
3
4
5
6
INP
LDA
LDB
MUL
STR
PNT
STP
number 7
number 7
number 7
square 8
square 8
Sample Program #1
Step 6: Final Assembly code
INP
LDA
LDB
MUL
STR
PNT
STP
7
7
7
8
8
Running the Code on the Model
Assembler

Type the code on the previous slide into a
file (use Notepad).

Save the file as sample1.txt in the same
directory as the assembler.exe file

Double click assembler.exe

Press ENTER

Type the filename sample1.txt

Press “r” to run
Before running the code, the screen will look like this:
Your Assembly Code
After running the code, the screen will look like this:
Results of Program Run
C++ Decisions
to Assembly Language
C++ Statement
if ( Num < 10 )
cout << Num;
Assembly equivalent
LDA Num
LDB Ten
CMP
[test condition]
JLT Then block address
JMP address of statement after Then block
PNT Num
[Then block]
C++ Decisions to Assembly Language
Pascal Statement
if ( Num < 10 )
cout << Num;
else
cout << “0”;
Assembly equivalent
LDA Num
LDB Ten
CMP
[Test condition]
JLT Then block address
PNT Zero
[Else block]
JMP Address of statement after Then block
PNT Num
[Then block]
Sample Program #2
Program #2.
Write an assembly program that will
get a number from the user, and
determine if the number is evenly
divisible by 5.
Output zero (false) if the number is
NOT evenly divisible by 5 or one (true)
if the number IS evenly divisible.
Sample Program #2
Step 1: Write the algorithm to describe the steps
needed to solve our problem.
1. Read in a number and store it in memory.
2. Determine if input number is evenly divisible by 5.
2.1 Divide input number by 5 to get the remainder.
2.2 Compare remainder to 0.
If remainder equals 0,
the number is evenly divisible.
If the remainder does not equal 0,
the number NOT evenly divisible.
3. Output the results
3.1 If evenly divisible, output 1.
3.2 If NOT evenly divisible, output 0.
Sample Program #2
Step 2: Write the C++ code
{
const int Zero = 0;
const int One = 1;
const int Five = 5;
int number, rem;
cout << "Enter number: ";
cin >> number;
rem = number % Five;
if (rem = Zero)
cout << One;
else
cout << Zero;
}
Sample Program #2
Step 3: Translate C++ code to Assembly
cout << "Enter number: ";
cin >> number;
rem = number % Five ;
INP number
LDA number
LDB Five
MOD
STR rem
Sample Program #2
Step 3: continued
if (rem = Zero)
cout << One;
else
cout << Zero;
}
LDA Zero
LDB rem
CMP
JEQ then block address
PNT Zero
else block
JMP address after then block
PNT One
then block
STP
Sample Program #2
Step 4: Number assembly code lines
starting from 0
0
1
2
3
4
5
6
7
8
9
10
11
12
INP
LDA
LDB
MOD
STR
LDA
LDB
CMP
JEQ
PNT
JMP
PNT
STP
number
number
Five
rem
Zero
rem
condition
then block address
Zero
else block
address after then block
One
then block
Sample Program #2
Step 5: Replace names by cell numbers after STP
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
INP
LDA
LDB
MOD
STR
LDA
LDB
CMP
JEQ
PNT
JMP
PNT
STP
5
0
1
number 16
number 16
Five 13
rem 17
Zero 14
rem 17
then block address
Zero 14
address after then block
One 15





Five
Zero
One
number
rem
Sample Program #2
Step 5: Replace jumps by instruction numbers
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
INP
LDA
LDB
MOD
STR
LDA
LDB
CMP
JEQ
PNT
JMP
PNT
STP
5
0
1
16
16
13
17
14
17
address of then block 11
14
else block
address after then block 12
15
then block
Sample Program #2
Step 6: Final Assembly code
INP
LDA
LDB
MOD
STR
LDA
LDB
CMP
JEQ
PNT
JMP
PNT
STP
5
0
1
16
16
13
17
14
17
11
14
12
15
C++ Decisions to Assembly Language
C++ Statement
while ( Num < 10 )
cout << Num;
Assembly equivalent
LDA Num
LDB Ten
CMP
 test condition
JLT to While Block
JMP to stmt after While Block
PNT Num
 While Block
JMP to test condition
 stmt after While Block
Sample Program #3
Program #3.
Write a program to display a count
by fives to 100.
Sample Program #3
Step 1: Write an algorithm to describe the steps
needed to solve our problem
1. Set Count to start at 0
2. While Count is less than 100
2.1 Add 5 to the Count and store the sum
back into the Count
2.2 Display the Count
Sample Program #3
Step 2: Write C++ code
{
const int Five = 5;
const int Hundred = 100;
int Count = 0;
while (Count < Hundred)
{
Count = Count + 5;
cout >> Count;
}
}
Sample Program #3
Step 3: Translate C++ code to assembly
while (Count < Hundred)
{
Count = Count + 5;
cout >> Count;
}
LDA Count
LDB Hundred
CMP
JLT to while block
JMP to stmt after while block
LDB Five
ADD
STR Count
PNT Count
JMP to test condition
 test condition
 while block
 stmt after while block
Sample Program #3
Step 4: Number assembly code lines from 0
0 LDA Count
 test condition
1 LDB Hundred
2 CMP
3 JLT to while block
4 JMP to stmt after while block
5 LDB Five
 while block
6 ADD
7 STR Count
8 PNT Count
9 JMP to test condition
10 STP
 stmt after while block
Sample Program #3
Step 5: Replace memory names with empty memory
locations after STP
0 LDA Count 13
1 LDB Hundred 12
2 CMP
3 JLT to while block
4 JMP to stmt after while block
5 LDB Five 11
6 ADD
7 STR Count 13
8 PNT Count 13
9 JMP to test condition
10 STP
11 5
 Five
12 100
 Hundred
13 0
 Count
Sample Program #3
Step 5: Replace memory names with empty memory
locations after STP
0 LDA 13
 test condition
1 LDB 12
2 CMP
3 JLT to while block 5
4 JMP stmt after while block 10
5 LDB 11
 while block
6 ADD
7 STR 13
8 PNT 13
9 JMP to test condition 0
10 STP
 stmt after while block
11 5
12 100
13 0
Sample Program #3
Final Assembly Code:
LDA 13
LDB 12
CMP
JLT 5
JMP 10
LDB 11
ADD
STR 13
PNT 13
JMP 0
STP
5
100
0