Вы находитесь на странице: 1из 27

Intrroduction

Architecture
Programming Techniques

on Set
Machine Language

Memories Instructio
Assemble Language
Assembler
Programming Step

mming
Data Transfer & Arithmetic Operations
p

Program
Data Manipulation
Decision Making

Back
Machine Language
 The actual binary instruction for a microprocessor is called
machine code
 Machine code = op code & operand(s)
Assembly Language
 A word like abbreviation of the instruction’s function are called
instruction mnemonics
 Mnemonics => > LD = LOAD = MOV = MOVE
 An example of Z80-CPU Instruction Set
Machine Code Mnemonic T-States Description
80 ADD A, B 4 A← A + B
A0 AND A, B 4 A← A ^ B
3D DEC A 4 A← A – 1
3C INC A 4 A← A + 1
C2 nn JP NZ , nn 10/7 If Z=0 then PC = nn
CA nn JP Z, nn 10/7 If Z=1 then PC = nn
78 LD A, B 4 A← B
79 LD A
A, C 4 A← C
32 nn LD (nn), A 13 (nn) ← A
Assembler
 Mnemonics must be interpreted into machine language by
human or by computer programming
 The computer program which converts mnemonics into machine
executable instructions is called an assembler.

 Writing Programs Flow Diagram


Programming Step
 Step
St iinvolve
l when
h ddesign
i an assembly
bl language
l programming
i
1. Problem Definition
• Identified the Algorithm
– Steps
p and sequence
q to solve pproblems
2. Flow Chart
• Draw the Flow Chart

Start

Decision

Process

Output
p
Display
Programming Step
3. Design Code (mnemonics)
• Using Microprocessor Instruction Set - come out the series
instruction needed.

4. Implementation
• Assemble mnemonics into machine code (binary code)
• Apply the code.

5. Testing and Debugging


• Test the outcome of the program
• Problem occur, debug what is the problem and run test again.
• Solve than, the program done successful.
Data Transfer Instructions
 Mainly involves the use of the Instruction LOAD (LD).
(LD)
DATA
moves or copy or write
i
Source Destination

Data Transfer

Data to/from Register Data to/from Memory

R2R R2M M2R M2M

Special

PUSH and POP IN and OUT


Examples Applications
1. Creating data in Register
 - LD A, 32H
2 Moving data from reg.
2. reg to reg.
reg
 - LD A, B
3. Creating data in memory
ORG 1800H
LD A, 76H
LD (1870H), A
RST38H
4. Moving Data from Memory to Memory
 Direct memory to memory.
 Using pointer (16-bit reg. pair to hold the memory address).
 Using Index Register IX and IY
 Usingg special
p instructions
- LDIR
Example
Write instruction to load 97H into the accumulator, 2050H into HL registers, and
2075H into the index register
g IX. Copy
py the contents of the accumulator into
register C and the contents of register H into register B. Write the HALT instruction
at the end of the sequence. Enter the machine codes of these instructions in
R/W memory starting from 1800H and show the contents of each register after
the execution.
execution
Memory Address Hex Code Opcode Operand
1 1800 3E
---- LD A, 97H
1801 97
2 1802 21
1803 50 LD HL, 2050H
1804 20
3 1805 DD
1806 21
LD IX, 2075H
1807 75
1808 20
4 1809 79 LD C, A
5 180A 44 LD B
B, H
6 180B 76 HALT
Example
Write instruction to load 97H into the accumulator, 2050H into HL registers, and
2075H into the index register
g IX. Copy
py the contents of the accumulator into
register C and the contents of register H into register B. Write the HALT instruction
at the end of the sequence. Enter the machine codes of these instructions in
R/W memory starting from 1800H and show the contents of each register after
the execution.
execution

Step 1 – Problem Definition – Generate the algorithm


2nd - load 97H into Acc.
3rd - load 2050H into reg. HL
4th - load 2075H into reg. IX
5th - copy A into C
6th - copy H into B
7th - HALT at end program
1st - Start at 1800H
Example
Write instruction to load 97H into the accumulator, 2050H into HL registers, and
2075H into the index register
g IX. Copy
py the contents of the accumulator into
register C and the contents of register H into register B. Write the HALT instruction
at the end of the sequence. Enter the machine codes of these instructions in
R/W memory starting from 1800H and show the contents of each register after
the execution.
execution

Step 2 – Draw the flow chart Step 3 – Design Code (mnemonics)


START Opcode Operand

Origin at 1800H ORG 1800H

Load 97H into Acc. LD A, 97H

Load 2050H into HL LD HL, 2050H

Load 2075H into IX LD IX, 2075H

Copy A into C LD C, A

Copy H into B LD B, H

HALT HALT
END
Step 4 – Implementation

Memory Address Hex Code Opcode Operand


1 1800 3E
LD A, 97H
1801 97
2 1802 21
1803 50 LD HL, 2050H
1804 20
3 1805 DD
1806 21
LD IX, 2075H
1807 75
1808 20
4 1809 4F LD C, A
5 180A 4B LD B, H
6 180B 76 HALT
Example
Write instruction to load 97H into the accumulator, 2050H into HL registers, and
2075H into the index register
g IX. Copy
py the contents of the accumulator into
register C and the contents of register H into register B. Write the HALT instruction
at the end of the sequence. Enter the machine codes of these instructions in
R/W memory starting from 1800H and show the contents of each register after
the execution.
execution
Memory Address Hex Code Opcode Operand
1 1800 3E
LD A,, 97H
1801 97
2 1802 21
1803 50 LD HL, 2050H
1804 20
3 1805 DD
1806 21
LD IX 2075H
IX,
1807 75
1808 20
4 1809 79 LD C, A
5 180A 44 LD B, H
6 180B 76 HALT
Data Transfer Operations

LD

LD

LD

LD
LD
Data Manipulation Instructions

 Also known as Arithmetic & Logic Instructions/ Operations


 Uses the ALU in the CPU to carry out these instruction
 For 8-bit operations one of the operand must be kept in the
accumulator or Reg. A
 For 16-bit
16 bit operations one of the operand is kept in register pair.
pair
e.g. HL
Data Manipulations Instructions

Data Manipulations

Arithmetic Logic Bit Shift & Rotate

 Arithmetic :- ADD, ADC, SUB, SBC, CP, INC,DEC, DAA


 Logic :- AND, OR, XOR, NEG, CPL
 Bit ::- BIT,
BIT SET
SET, RES
 Shift & Rotate :- SLA, SRA, SRL, RLC, RRC, RR, RL
Examples Applications
1. Adding two numbers in stored registers
2. Subtracting two numbers stored in registers
3. - Adding
g two number stored in memoryy
ORG 1800H
LD A, (1850H)
LD B, A
LD A, (1851H)
ADD A, B
RST 38H
- Adding
Addi numbers
b iin memory using
i register
i t HL as pointer.
i t
ORG 1800H
LD HL,1850H
LD A (HL)
A,(HL)
INC HL
LD B,(HL)
ADD A,B
RST 38H
Examples Applications
- Adding numbers in memory using Index Register as pointer
ORG 1800H
LD IX,1850H
LD A,(IX+00H)
ADD A,(IX+01H)
RST 38H
4. Incrementing and decrementing the value in a register
5. Comparing two numbers stored in registers and finding the equality, greater
than and less than.
6. ANDing, ORing and XORing two numbers in stored registers
7. Complementing (1’s and 2’s) number in a register
8
8. Testing a bit,
bit resetting and setting it
9. Shifting and Rotating data in register
Examples
 Load two numbers F2H and 68H in registers B and C,
respectively, and store A2H in memory location 2065H, using
the HL register as memory pointer.
pointer Subtract 68H from F2H,
F2H
complement the result, then add with A2H from memory. Store
the final answer in memory location 2066H. Show register
contents and the status of S (Sign),
(Sign) Z (Zero),
(Zero) and C (Carry)
flags as each instruction is being executed.
 Solution:-
Decision Making Instructions
 Instructions that will alter the flow of program execution. This will
enable certain part of a program to be skipped, or certain part of
a program to be repeated many times (looping)
Decision Making
Is made Is made
depending regardless
Conditional Unconditional
on the of the CPU
CPU status
status JUMP (JP
(JP, JR)

CALL & RETURN

RESTART (RST)
 Status of a CPU is kept in Flag Register or Status Register
Examples Applications (1)
1
1. Add 10 bbytes
t off ddata
t in
i memory
 e.g. Find the Total sum of numbers in memory address 1820H until 1829H.
ORG 1800H
LD HL, 1820H ; set memory pointer
LD B, 0AH ; set counter
LD A, 00H ; clear acc.
LOOP LD C, (HL) ; fetch no.
ADD A, C ; add up
INC HL ; adjust pointer location
DJNZ LOOP ; if not finished, continue
RST 38H

2. 2’s complement 10 data in memory


 e.g. 2’s complement the number in memory from 1900H until 1909H.
ORG 1800H
LD HL, 1900H ; set memory pointer
LD B, 0AH ; set counter
LD A, 00H ; clear acc.
LOOP LD A, (HL) ; fetch no.
NEG ; 22’ss complement
LD (HL),A ; SAVE BACK
INC HL ; adjust pointer location
DJNZ LOOP ; if not finished, continue
RST 38H
Examples Applications (2)
1
1. Copy 10 Bytes of data from one part of memory to another part of memory (block copy)
 Transfer/ copy 10 bytes data starting from memory 1820H into memory 1850H.
ORG 1800H
LD HL, 1820H ; set memory pointers (source)
LD DE,, 1850H ; set ppointer ((destination))
LD B, 0AH ; set counter
LOOP LD A, (HL) ; fetch no. from source
LD (DE), A ; transfer no. to destination
INC HL ; adjust pointer location sources
INC DE ; adjust pointer location destination
DJNZ LOOP ; if nott finished,
fi i h d continue
ti ttransfer
f
RST 38H
2. Finding the largest number in an array of numbers
 e.g. Find the largest no. from an array 1820H – 1823H and store the largest no. found into 1900H
ORG 1800H
LD HL, 1820H ; set memory pointers
LD B, 04H ; set counter
LD D, 00H ; clear temporary location
LOOP LD A, (HL) ; get no.
CP D ; compare
p for biggergg no.
CALL S, STR_BIG ; if S=1, => STORE IN 1900H
INC HL ; Adjust pointer
LD D, A ; store no. in reg D to compare with next value
DJNZ LOOP ; repeat until finish
RST 38H

STR_BIG LD A,D ; store temporarily the largest no. in accumulator


LD (1900H),A ; Save the largest value/ no. in memory 1900H
RET ; return to main prog.
Examples Applications (3)
1. Finding the smallest number in an array of numbers
2. Finding how many numbers has the value between 3AH to 8FH in an
array of numbers
3. Convert an 8 bit number into 2 digit BCD number
4. Multiply two numbers together using successive addition method
 Multiply
p y Reg.
g A. with Reg.
g B,, answer is in Reg.
g A
ORG 1800H
LD C, A ; Set up multiplier in C
LD A, 00H ; Clear sum location
LOOP ADD A, C ; Add up
DEC B ; decrement multiplicand
JP NZ, LOOP ;if not finished, repeat
RST 38H
Stack Pointer (SP) and Subroutines
 Stack Pointer – 16 bit register used to point to memory address
called stack
 Associated with PUSH and POP
 SP normally loaded with an address of highest value

 Subroutine – Short sequence of instructions that perform a


single task.
 Can be used many time, without being stored more than once.
 Jump to is - Initiated by CALL instruction
 Jump
J back
b k iis – byb RET instruction
i t ti
Examples Applications
1. Delay
D l subroutine
b ti
ORG 1900H ; subroutine delay at location 1900H
DELAY LD B, 0FFH ; set count
LOOP NOP ; do nothingg
DEC B ; decrement count
JR NZ, LOOP ; if count NZ, repeat
RET ; return to main program
 A longer
g time delayy can be obtained byy usingg 16-bit counter.. Reg.
g Pair BC
 Decrement Reg. BC does not change or affect the Z flag…, a few instructions
are used to test reg. pair BC whether it is zero or not
ORG 1900H
DELAY LD BC XXXXH
BC, ; set count,
count maximum for xxxx
; is FFFFH
DEL DEC BC ; Decrement count
LD A, B
OR
O C ; test if cou
counter
te iss zero
eo
JR NZ, DEL ; not zero, repeat again
RET
2. Multiplication Program
3 Division Program
3.
4. Data Conversion Program
Examples Applications
1. Delay subroutine – To xxxxH is a number
calculate delay time of looping in either
in decimal or
T STATE
T- hexadecimal
ORG 1900H -
DELAY LD BC, XXXXH 10
DEL DEC BC 6
LD A, B 4
‘DEL’ LOOP
OR C 4
JR NZ, DEL 12
RET 10
TOTAL T – STATE IN ‘DEL’ LOOP 26
TOTAL T – STATE OUT ‘ DEL’
20
LOOP
Examples Applications
1. Delay subroutine – To
calculate delay time

DELAY TIME = [TIME IN LOOP] + [TIME OUT OF LOOP]

TIME IN LOOP = [TCLOCK x Total TState in loop x No.


No of looping]

TIME OUT OF LOOP = [TCLOCK x Total Tstate out of loop ]

Вам также может понравиться