assembly language programming cs208. assembly language assembly language allows us to use convenient...
Post on 20-Dec-2015
267 views
TRANSCRIPT
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 theFetch-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 if (A < B)
0 if (A = B)
1 if (A > B)
Model Assembly Language InstructionsOperation What it means to the CPU
STP Stop the program
LDA Load register A with value from a specified memory location
LDB Load register B with value from a specified memory location
STR Store register B value to a specified memory location
INP Store data input by user to a specified memory location
PNT Print the value stored in a specified memory location to the screen
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) andstore 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
Model Assembly Language Instructions
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 LanguageStatement Assembly equivalent
#include none
void main() none
const value in memory cell
int, double, char address of memory cell
cin INP
cout PNT
assignment (=) LDA Val1 val3 = val1 + val2 LDB Val2
ADDSTR 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;
square = number * number;
cout << Square;
}
INP number
LDA numberLDB numberMULSTR square
PNT square
STP
Sample Program #1
Step 4: Number assembly code lines starting from 0
0 INP number1 LDA number2 LDB number3 MUL4 STR square5 PNT square6 STP
Sample Program #1
Step 5: Replace memory names with empty memory locations after STP
0 INP number 71 LDA number 72 LDB number 73 MUL4 STR square 85 PNT square 86 STP
Sample Program #1
Step 6: Final Assembly code
INP 7LDA 7LDB 7MULSTR 8PNT 8STP
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++ Decisionsto Assembly Language
C++ Statementif ( Num < 10 )
cout << Num;
Assembly equivalentLDA NumLDB TenCMP [test condition]JLT Then block addressJMP address of statement after Then blockPNT Num [Then block]
C++ Decisions to Assembly Language
Pascal Statementif ( Num < 10 )
cout << Num;else
cout << “0”;
Assembly equivalentLDA NumLDB TenCMP [Test condition]JLT Then block addressPNT Zero [Else block]JMP Address of statement after Then blockPNT 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 #2Step 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 #2Step 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; INP number
LDA numberrem = number % Five ; LDB Five
MODSTR rem
Sample Program #2
Step 3: continued
if (rem = Zero) LDA Zero
cout << One; LDB rem
else CMP
cout << Zero; JEQ then block address
PNT Zero else block
JMP address after then block
PNT One then block
} STP
Sample Program #2Step 4: Number assembly code lines
starting from 0
0 INP number1 LDA number2 LDB Five3 MOD4 STR rem5 LDA Zero condition6 LDB rem7 CMP8 JEQ then block address9 PNT Zero else block10 JMP address after then block11 PNT One then block12 STP
Sample Program #2Step 5: Replace names by cell numbers after STP
0 INP number 161 LDA number 162 LDB Five 133 MOD4 STR rem 175 LDA Zero 146 LDB rem 177 CMP8 JEQ then block address9 PNT Zero 1410 JMP address after then block11 PNT One 1512 STP13 5 Five14 0 Zero15 1 One16 number17 rem
Sample Program #2Step 5: Replace jumps by instruction numbers
0 INP 161 LDA 162 LDB 133 MOD4 STR 175 LDA 146 LDB 177 CMP8 JEQ address of then block 119 PNT 14 else block10 JMP address after then block
1211 PNT 15 then block12 STP13 514 015 11617
Sample Program #2Step 6: Final Assembly code
INP 16LDA 16LDB 13MODSTR 17LDA 14LDB 17CMPJEQ 11PNT 14JMP 12PNT 15STP501
C++ Decisions to Assembly Language
C++ Statementwhile ( Num < 10 )
cout << Num;
Assembly equivalentLDA NumLDB TenCMP 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 #3Step 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 #3Step 3: Translate C++ code to assemblywhile (Count < Hundred) {
Count = Count + 5;cout >> Count;
}
LDA Count test conditionLDB HundredCMPJLT to while blockJMP to stmt after while blockLDB Five while blockADDSTR CountPNT CountJMP to test condition
stmt after while block
Sample Program #3Step 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 #3Step 5: Replace memory names with empty memory
locations after STP 0 LDA Count 131 LDB Hundred 122 CMP3 JLT to while block4 JMP to stmt after while block5 LDB Five 116 ADD7 STR Count 138 PNT Count 139 JMP to test condition10 STP11 5 Five12 100 Hundred13 0 Count
Sample Program #3Step 5: Replace memory names with empty memory
locations after STP
0 LDA 13 test condition1 LDB 122 CMP3 JLT to while block 54 JMP stmt after while block 105 LDB 11 while block6 ADD7 STR 138 PNT 139 JMP to test condition 010 STP stmt after while block11 512 10013 0
Sample Program #3Final Assembly Code: LDA 13LDB 12CMPJLT 5JMP 10LDB 11ADDSTR 13PNT 13JMP 0STP51000