assembly language programming cs208. assembly language assembly language allows us to use convenient...

45
Assembly Language Programming CS208

Post on 20-Dec-2015

251 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

Assembly Language Programming

CS208

Page 2: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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.

Page 3: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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.

Page 4: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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.

Page 5: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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.

Page 6: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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).

Page 7: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

CPU

Page 8: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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.

 

Page 9: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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.

Page 10: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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)

Page 11: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 12: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 13: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 14: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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)

Page 15: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 16: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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.

Page 17: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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.

Page 18: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

Sample Program #1

Step 2: Write the C++ code

{

int Number , Square;

cout << "Enter a number: ";

cin >> Number;

Square = Number * Number ;

cout << Square;

}

Page 19: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 20: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 21: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 22: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

Sample Program #1

Step 6: Final Assembly code

INP 7LDA 7LDB 7MULSTR 8PNT 8STP

Page 23: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 24: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

Before running the code, the screen will look like this:

Your Assembly Code

Page 25: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

After running the code, the screen will look like this:

Results of Program Run

Page 26: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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]

Page 27: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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]

Page 28: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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.

Page 29: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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.

Page 30: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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;}

Page 31: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 32: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 33: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 34: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 35: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 36: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

Sample Program #2Step 6: Final Assembly code

INP 16LDA 16LDB 13MODSTR 17LDA 14LDB 17CMPJEQ 11PNT 14JMP 12PNT 15STP501

Page 37: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 38: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

Sample Program #3

Program #3.

Write a program to display a count by fives to 100.

Page 39: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 40: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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;

}}

Page 41: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 42: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 43: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 44: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

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

Page 45: Assembly Language Programming CS208. Assembly Language Assembly language allows us to use convenient abbreviations (called mnemonics) for machine language

Sample Program #3Final Assembly Code: LDA 13LDB 12CMPJLT 5JMP 10LDB 11ADDSTR 13PNT 13JMP 0STP51000