1. Trang chủ
  2. » Kỹ Thuật - Công Nghệ

8051 - Thí dụ về tập lệnh

42 379 0
Tài liệu được quét OCR, nội dung có thể không chính xác
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề 8051 - Thí Dụ Về Tập Lệnh
Trường học University of Technology
Chuyên ngành Computer Science
Thể loại Bài Tập
Thành phố Hà Nội
Định dạng
Số trang 42
Dung lượng 683 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Access RAM Locations Using Register Solution: After the execution of the above program we have the following: RAM location 0 has value 99H RAM location 1 has value 85H RAM location 2 has

Trang 1

MOV Instruction

MOV destination, source ; copy source to

9Í 18 MOV A,#55H ;load value 55H into reg A

MOV ROA ‘copy contents of A into RO

‘(now A=RO=55H)

MOV R1,A ‘copy contents of A into R1

‘(now A=RO=R1=55H) MOV R2,A ‘copy contents of A into R2

‘(now A=RO=R1=R2=55H) MOV R3,#95H ;:load value 95H into R3

‘(now R3=95H)

MOV A,R3 ‘copy contents of R3 into A

‘now A=R3=95H

Trang 2

Notes on Programming

¢ Value (proceeded with #) can be loaded

directly to registers A, B, or RO — R7

—MOV E5, #0F9H

¢ If values 0 to F moved into an 8-bit

register, the rest assumed all zeros

—-MOV A, #9

¢ A too large value causes an error

—MOV A, #/7F2H

Trang 3

ADD Instruction

¢ ADD A, source ;ADD the source

0operand

‘to the accumulator

¢ MOV A, #25H ‘load 25H into A

MOV R2,#34H — ;load 34H into R2

ADD A,R2 ‘add R2 to accumulator

(A =A+R2)

Trang 4

ADD Instruction and PSW

67 01100111

CY =0since there is no carry beyond the D7 bit

AC = | since there is a carry from the D3 to the D4 bit

P = 1 since the accumulator has an odd number of Is (it has five Is)

Trang 5

ADD Instruction and PSW

Trang 6

ADD Instruction and PSW

Example 2-4

Show the status of the CY, AC, and P flags after the addition of 88H and 93H in the fol- lowing instructions

MOV A, #88H ADD A, #93H ;after the addition A=1BH,CY=1

Solution:

88 10001000

+93 10010011 11B 00011011

CY=I since there is a carry beyond the D7 bit

AC=0 since there is no carry from the D3 to the D4 bit

P=0 since the accumulator has an even number of Is (it has four 1s)

Trang 7

otructure of Assembly Language

ORG OH start (origin) at location O MOV R5,#25H — ;load 25H into R5 MOV R/,#34H — ;load 34H into R/

HERE: SJMP HERE ‘stay in this loop

Trang 8

DB 2591 , ASGII NUMBEES ORG 518H

DB ‘My name is Joe’ ;ASCII CHARACTERS

Trang 9

Access RAM Locations Using Register

Solution:

After the execution of the above program we have the following:

RAM location 0 has value 99H RAM location 1 has value 85H

RAM location 2 has value 3FH RAM location 7 has value 63H

Trang 10

Access RAM Locations Using

MOV 02,#3FH jload R2 with value 3FH

MOV 07,#63H jload R? with value 63H MOV 05, #12H j;load R5 with value 12H

Trang 11

Switch Register Banks

Example 2-7

State the contents of the RAM locations after the following program:

SETB PSW.4 ;select bank 2 |

MOV RO, #99H ;load RO with value 99H

MOV R1,#85H ;load Rl with value 85H MOV R2,#3FH j;load R2 with value 3FH

MOV R7,#63H ;load R7 with value 63H

MOV RS,#12H j;load R5 with value 12H

Solution:

By default, PSW.3=0 and PSW.4=0; therefore, the instruction “SETB PSW.4” sets

RS1=1 and RSO=0, thereby selecting register bank 2 Register bank 2 uses RAM loca-

tions 10H - 17H After the execution of the above program we have the following:

RAM location 10H has value 99H RAM location 11H has value 85H

RAM location 12H has value 3FH RAM location 17H has value 63H

RAM location 15H has value 12H

Trang 12

Pushing onto Stack

Trang 13

Popping ied stack

Examining the stack, show the contents of the registers and SP Rer execution of the

following instructions All values are in hex

POP 3 ;POP stack into R3

POP 5 ;POP stack into R5

POP 2 7;POP stack into R2-

Trang 14

Stack & Bank 1 Conflict

Show the stack and stack pointer for the following instruetions

MOV SP,#5FH ;make RAM location 60H

;first stack location

MOV R2,#25H MOV R1,#12H MOV R4,#OE3H PUSH 2

PUSH 1 PUSH 4

Trang 15

a TC fay a

Trang 16

Arithmetic Instructions and

Programs

Trang 17

Outlines

Range of numbers in 8051 unsigned data

Addition & subtraction instructions for unsigned data

BCD system of data representation

Packed and unpacked BCD data

Addition & subtraction on BCD data

Range of numbers in 8051 signed data

Signed data arithmetic instructions

Carry & overflow problems & corrections

Trang 18

Addition of Unsigned Numbers

Trang 19

|Example là! |

Show how the flag register is affected by the following instructions

MOV A, #0F5H ;A=F5 hex

After the addition, register A (destination) contains 00 and the flags are as follows:

CY = 1 since there is a carry out from D7

PF = | because the number of Is is zero (an even number), PF is set to 1

AC = | since there is a carry from D3 to D4

Trang 20

Addition of Individual Bytes

Assume that RAM locations 40 - 44 have the following values Write a program to find

the sum of the values At the end of the program, register A should contain the low byte and R7 the high byte All values are in hex

Solution:

AGAIN:

NEAT:

40=(7D) 41=(EB) 42=(C5) 43= (SB) 44=(30)

MOV RO, #40H MOV R2,#5 CLR A

MOV R7,A ADD A, @RO JNC NEXT INC R?

INC RO

DINZ RZ, T1.“

;load pointer jload counter ©

rw Oe TH

;clear ve

;add the án —- rote) by RO

iff CY=0 don’t accumulate carry

;keep track of carries

gincrement pointer = Frepeat mntii are ` ÁN

Trang 21

ADDC & Addition of 16-bit Numbers

3C oi rans: IB ik: 74

L 6-3

Write a program to add two 16-bit numbers The numbers are 3CE7H and 3B8DH

Place the sum in R7 and R6; R6 should have the lower byte

Solution:

CLR Cc ;zmake CY=0

MOV A, #O0E7H jload the low byte now A=E7H

ADD A, #8DH ;add the low byte now A=74H and CY=1

MOV R6,A ;save the low byte of the sum in R6

MOV A, #3CH ;load the high byte

ADDC A, #3BH ;add with the carry

;3B + 3C + 1 = 78(all in la

MOV R7,A zsave the high byte of the sum

Trang 23

Adding BCD Numbers & DA Instruction

MOV A,#47H -A=47H first BCD operand MOVB,#25H ‘-B=25 second BCD operand

ADD AB ‘hex (binary) addition (A=6CH)

DA A ‘adjust for BCD addition (A=/2H)

29 0010 1001 + 18 +0001 1000

AT 0100 0111

Trang 24

Example

Example 6-4

Assume that 5 BCD data items are stored in RAM locations starting at 40H, as shown

below Write a program to find the sum of all the numbers The result must be in BCD

40={71) 41= (11) 432= (65)

MOV R/,A ;Clear R7

AGAIN: ADD A,@RO ;adda the byte pointer to by RO

DA AD len for BCD

INC NEXT z1£ CY=0 don’t accumulate carry

INC R? ;keep track of carries NEXT: INC RO j;increment pointer

DJINZ R2, AGAIN ;repeat until R2 is zero

Trang 25

Subtraction of Unsigned Numbers

¢ SUBB A, source ; A= A-— source — CY

¢ SUBB when CY =0

— Take 2’s complement of subtraend (source)

— Add it to minuend

— Invert carry

Trang 26

Example (Positive Result)

MOV A,#3FH jload 3FH into A (A=3FH)

MOV R3,#23H jload 23H into R3(R3=23H)

SUBB A,R3 ;zSubtract A - R3, place result inA

The flags would be set as follows: CY = 0, AC = 0, and the programmer must look at

the carry flag to determine if the result is positive or negative

Trang 27

Example (Negative Result)

[Example 6-6

Analyze the following program:

CLR C

MOV A, #4C ;load A with value 4CH (A=4CH)

SUBB A, #6EH ;subtract 6E from A

ỨNC NEXT ;if CY=0 jump to NEXT target

CPL A ;if CY=1 then take 1’s complement

INC A jand increment to get 2’s complement

NEXT:MOV RI1,A ;save A in Rl

Trang 28

MOV A,#27H ;A=27H SUBB A, #12H © ;27H-12H-1=14H

MOV RO6,A ;save the result

Solution:

After the SUBB, A = 62H - 96H = CCH and the carry flag is set high indicating there

is a borrow Since CY = 1, when SUBB is executed the second time A = 27H -— 12H -

1 =14H Therefore, we have 2762H — 1296H = 14CCH

Trang 29

Multiplication of Unsigned Numbers

¢ MUL Vẫn, ẳ ÄAxBö, place 16-bIf result in B and A

MOVA,#25H ‘load 25H to reg A

MOVB,#65H ‘load 65H in reg B

MUL AB ‘20H * 65H = E99 where

Trang 30

Division of Unsigned Numbers

MOVA,#95H ‘load 95 into A

MOVB,#10H ‘load 10 into B

DIV AB ‘now A = 09 (quotient) and

Trang 31

Example ( 1 of 2 )

(a) Write a program to get hex data in the range of 00 - FFH from port 1 and convert it

to decimal Save the digits in R7, R6, and R5, where the least significant digit is in R7 (b} Analyze the program, assuming that P1 has a value of FDH for data

Solution:

(a)

MOV A,#OFEH MOV PI1,A ;make Pl an input port MOV A,P1 ;read data from Pl

MOV B, #10 ;zB=0A hex (10 dec) DIV AB divide by 10

MOV R7,B ;save lower digit MOV B, #10 h

DIV AB ;divide by 10 once more

MOV R6,B ;save the next digit

MOV R5,A ;save the last digit

Trang 32

Example ( 2 of 2 )

(b)

To convert a binary (hex) value to decimal, we divide it by 10 repeatedly until the quo-

tient is less than 10 After each division the remainder is saved In the case of an 8-bit

binary such as FDH we have 253 decimal as shown below (all in hex)

N Rr FD/0A= 19 3 (low digit)

19/0A= 2 5 (middle digit)

2 (high digit)

Therefore, we have FDH = 253 In order to display this data it must be converted to ASCII, which is described in the next chapter, -

Trang 33

Signed 8-bit Operands

Trang 34

VĂN 1111 1010 invert each bit

3 1111 1011 add 1 (which becomes FB in hex)

Therefore -5 = FBH, the signed number representation in 2’s complement for -5

Trang 35

Tan 1100 1011 invert each bit

E 1100 1100 add 1 {which is CC in hex)

Therefore -34 = CCH, the signed number representation in 2’s complement for -34H

Trang 36

2 0111 1111 invert each bit

3 1000 0000 add 1 {which becomes 80 in hex)

Therefore -128 = 80H, the signed number representation in 2’s complement for -128

Trang 37

Byte-sized Signed Numbers

Trang 38

Overflow in Signed Number Operations

|Examplc 6-12

Examine the following code and analyze the result

MOV A, #+96 ;A=0110 0000 (A=60H) MOV R1,#+70 ;R1=0100 0110(R1=46H) ADD A,R]1 7A=1010 0110

;A=A6H= -90 decimal, INVALID!!) Solution:

+96 0110 0000 + +70 0100 0110

+ 166 1010 0110 and OV=l

According to the CPU, the result is -90, which is wrong The CPU sets OV = | to indi-

Trang 39

When Is the OV Flag Set?

¢ Either: there is a carry from D6 to D7 but

no carry out of D7 (CY = 0)

¢ Or: there is a carry from D7 out (CY = 1) but no carry from D6 to D7

Trang 40

Example

Observe the following, noting the role of the OV flag

MOV A, #-128 7;A=1000 0000 (A=80H)

Trang 41

Example

Observe the following, noting the OV flag

MOV A, ¥-2 ;A=1111 1110 (A=FEH)

Trang 42

Example

Examine the following, noting the role of OV

MOV A,#+? ;A=0000 0111 (A=07H)

Ngày đăng: 23/10/2013, 12:15

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w