assembly language programming. cpu the cpu contains a control unit, arithmetic logic unit (alu) and...

48
Assembly Language Programming

Post on 21-Dec-2015

246 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly Language Programming

Page 2: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

CPU

Page 3: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

CPU The CPU contains a Control Unit, Arithmetic Logic

Unit (ALU) and a small number of memory locations called Registers.

  Different registers perform different tasks such:

– as manipulating data, – keeping track of the results of decision making operations, – and pointing to the next instruction to be executed.

Page 4: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

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 5: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

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 6: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

CPU Registers The computer also has a Compare instruction that

can be used to compare the contents of register A with those of register B.

The comparison can have three possible outcomes: – the contents of register A < B; – the contents of the register A = B; – the contents of the register A > B.

Page 7: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

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 8: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

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 9: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

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 a one-to-one correspondence between the assembly languages mnemonic instructions and the machine language numeric instructions.

A list of assembly code instructions that will perform a task is called an assembly program.

Page 10: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Operation What it means to the CPU

STP Stop the program

LDA Load register A with contents of a specified memory location

LDB Load register B with contents of a specified memory location

STR Store register B contents to a specified memory location

INP Store data input by user to a specified memory location

PNT Print the contents of a specified memory location to the screen

Page 11: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly 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

Page 12: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly 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

Page 13: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Steps to write Assembly Programs Create Pascal Program translate each Pascal statement to the equivalent

assembly statement(s) Number the assembly language program starting

from 0 Replace Memory names by number of empty

memory cell Resolve jumps ( replace with number of memory

cell jumping to)

Page 14: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Pascal to Assembly language.Statement Assembly equivalent

program none

const put value in memory cell

var put address of memory cell

readln INP

writeln PNT

assignment (:=)

val3 = val1 + val2 LDA Val1

LDB Val2

ADD

STR Val3

End. STP

Page 15: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.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 16: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.step 1: algorithm to describe the steps needed to solve

our problem.

Page 17: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.step 1: algorithm to describe the steps needed to solve

our problem.

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. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 2: write Pascal code

Page 19: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 2: write Pascal code

Var

number , square: integer;

begin

readln ( number);

square := number * number ;

writeln (square);

end.

Page 20: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly Languagebegin

readln ( number);

square := number*number;

writeln (square);

end.

INP number

LDA number

LDB number

MUL

STR square

PNT square

STP

Page 21: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 4: Number assembly code lines starting from 0

0 INP number

1 LDA number

2 LDB number

3 MUL

4 STR square

5 PNT square

6 STP

Page 22: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 5: Replace memory names by cell numbers after

STP

0 INP number 7

1 LDA number 7

2 LDB number 7

3 MUL

4 STR square 8

5 PNT square 8

6 STP

Page 23: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 6: Final Assembly code

INP 07

LDA 07

LDB 07

MUL

STR 08

PNT 08

STP

Page 24: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Pascal to Assembly language.Statement Assembly equivalent

if ( N < 10 ) then LDA N (condition expression)

writeln (N) LDB Ten

CMP

JOP (operation) (Then Block)

JMP to statement after then block

PNT N ( then block)

Page 25: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Pascal to Assembly language.Statement Assembly equivalent

if ( N < 10 ) then LDA N (condition expression)

writeln (N) LDB Ten

else writeln (‘0’); CMP

JOP (operation) (Then Block)

PNT Zero ( else Block)

JMP to statement after then block

PNT N ( then block)

Page 26: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.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 27: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.step 1: algorithm to describe the steps needed to solve

our problem.

Page 28: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.step 1: algorithm to describe the steps needed to solve our

problem.

1. Input a number and store it in memory.

2. Determine if the input number is evenly divisible by 5.

2.1Divide the input number by 5 to get the remainder.

2.2 Compare the 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 29: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 2: write Pascal code

Page 30: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 2: write Pascal code

Const

Zero = 0; One =1; Five = 5;

Var

number , rem: integer;

begin

readln ( number);

rem := number MOD Five ;

if (rem = Zero) then

writeln (One);

else writeln (Zero)

end.

Page 31: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 3: translate Pascal code to assembly

Const

Zero = 0; One =1; Five = 5;

Var

number , rem: integer;

begin

readln ( number); INP number

rem := number MOD Five ; LDA number

LDB Five

MOD

STR rem

Page 32: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 3: translate Pascal code to assembly

if (rem = Zero) then condition exp LDA Zero

writeln (One); LDB rem

else writeln (Zero) CMP

end. JEQ then block

else block PNT Zero

JMP after then

then block PNT One

STP

Page 33: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 4: Number assembly code lines starting from 0

0 INP number1 LDA number2 LDB Five3 MOD4 STR rem

condition exp 5 LDA Zero6 LDB rem7 CMP8 JEQ then block

else block9 PNT Zero10 JMP after then

then block 11 PNT One12 STP

Page 34: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 5: Replace memory names by cell numbers after STP

0 INP number 161 LDA number 162 LDB Five 133 MOD4 STR rem 17

condition exp 5 LDA Zero 146 LDB rem 177 CMP8 JEQ then block

else block9 PNT Zero 1410 JMP after then

then block 11 PNT One 1512 STP

13 514 015 1

Page 35: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 5: Replace jumps by instruction numbers

0 INP 161 LDA 162 LDB 133 MOD4 STR 175 LDA 146 LDB 177 CMP8 JEQ then block 11

else block9 PNT 1410 JMP after then 12

then block 11 PNT 1512 STP13 514 015 1

Page 36: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 6: Final Assembly code

INP 16LDA 16LDB 13MODSTR 17LDA 14LDB 17CMPJEQ 11

PNT 14JMP 12PNT 15STP501

Page 37: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Statement Assembly equivalent

While (N < 10 ) do LDA N (condition expression)

begin LDB Ten

writeln (N) CMP

end; JOP (operation) (While Block)

JMP to statement after while block

PNT N ( while block statements)

JMP Condition

Page 38: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language. Program #3. Write an assembly program that will add up a series of

positive numbers entered by the user, until the user enters a negative number, then display the total.

Page 39: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.step 1: algorithm to describe the steps needed to solve

our problem.

Page 40: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.step 1: algorithm to describe the steps needed to solve

our problem.

1. Input a value and store it in memory. 2. While the Input Value is not a negative

number:– 2.1 Add the Input Value to the Running Total and store – the sum back into the Running Total.– 2.2 Input another value and store it in memory.

3. Output the contents of the Running Total.

Page 41: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 2: write Pascal code

Page 42: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 2: write Pascal code

Const

Zero = 0; Var

sum , number: integer;begin

sum := zero;readln ( number);While ( number >= Zero) dobegin

sum := sum + number;readln (number);

end; writeln (sum)

end.

Page 43: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 3: translate Pascal code to assembly

begin

sum := zero;

readln ( number);

While ( number >= Zero) do

LDB Zero

STR sum

INP number

condition LDA numberLDB Zero

CMP

JGT (While Block)

JEQ (While Block)

JMP after while block

Page 44: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 3: translate Pascal code to assembly

begin

sum := sum + number;

readln (number);

end;

writeln (sum)

end.

LDA sumLDB numberADDSTR sum

INP number

JMP Condition

PNT sum

STP

Page 45: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 4: Number assembly code lines starting from 0

0 LDB Zero

1 STR sum

2 INP number

condition 3 LDA number4 LDB Zero

5 CMP

6 JGT (While Block)

7 JEQ (While Block)

8 JMP after while block

9 LDA sum10 LDB number11 ADD12 STR sum

13 INP number

14 JMP Condition

15 PNT sum

16 STP

Page 46: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 5: Replace memory names by cell numbers after

STP0 LDB Zero 17

1 STR sum 18

2 INP number 19

condition 3 LDA number 194 LDB Zero 17

5 CMP

6 JGT (While Block)

7 JEQ (While Block)

8 JMP after while block

while body 9 LDA sum 1810 LDB number 1911 ADD12 STR sum 18

13 INP number 19

end while 14 JMP Condition

15 PNT sum 18

16 STP17 0

Page 47: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 5: Replace jumps by cell numbers

0 LDB 17

1 STR 18

2 INP 19

condition 3 LDA 19

4 LDB 17

5 CMP

6 JGT (While Block) 9

7 JEQ (While Block) 9

8 JMP after while block 15

while body 9 LDA 1810 LDB 1911 ADD12 STR 18

13 INP 19

end while 14 JMP Condition 3

15 PNT 18

16 STP17 0

Page 48: Assembly Language Programming. CPU The CPU contains a Control Unit, Arithmetic Logic Unit (ALU) and a small number of memory locations called Registers

Assembly language.Step 6: Final Assembly code

LDB 17

STR 18

INP 19

LDA 19

LDB 17

CMP

JGT 9

JEQ 9

JMP 15

LDA 18LDB 19ADDSTR 18INP 19JMP 3PNT 18STP0