MOV Instruction MOV destination source copy source to

  • Slides: 42
Download presentation
MOV Instruction MOV destination, source ; copy source to dest. MOV A, #55 H

MOV Instruction MOV destination, source ; copy source to dest. MOV A, #55 H MOV R 0, A ; load value 55 H into reg. A ; copy contents of A into R 0 ; (now A=R 0=55 H) MOV R 1, A ; copy contents of A into R 1 ; (now A=R 0=R 1=55 H) MOV R 2, A ; copy contents of A into R 2 ; (now A=R 0=R 1=R 2=55 H) MOV R 3, #95 H ; load value 95 H into R 3 ; (now R 3=95 H) MOV A, R 3 ; copy contents of R 3 into A ; now A=R 3=95 H

Notes on Programming • Value (proceeded with #) can be loaded directly to registers

Notes on Programming • Value (proceeded with #) can be loaded directly to registers A, B, or R 0 – R 7 – MOV R 5, #0 F 9 H • If values 0 to F moved into an 8 -bit register, the rest assumed all zeros – MOV A, #5 • A too large value causes an error – MOV A, #7 F 2 H

ADD Instruction • ADD A, source ; ADD the source ; operand ; to

ADD Instruction • ADD A, source ; ADD the source ; operand ; to the accumulator • MOV A, #25 H ; load 25 H into A MOV R 2, #34 H ; load 34 H into R 2 ADD A, R 2 ; add R 2 to accumulator ; (A = A + R 2)

ADD Instruction and PSW

ADD Instruction and PSW

ADD Instruction and PSW

ADD Instruction and PSW

ADD Instruction and PSW

ADD Instruction and PSW

Structure of Assembly Language ORG MOV MOV ADD 0 H ; start (origin) at

Structure of Assembly Language ORG MOV MOV ADD 0 H ; start (origin) at location 0 R 5, #25 H ; load 25 H into R 5 R 7, #34 H ; load 34 H into R 7 A, #0 ; load 0 into A A, R 5 ; add contents of R 5 to A ; now A = A + R 5 ADD A, R 7 ; add contents of R 7 to A ; now A = A + R 7 ADD A, #12 H ; add to A value 12 H ; now A = A + 12 H HERE: SJMP HERE ; stay in this loop END ; end of asm source file

Data Types & Directives DATA 1: DATA 2: DATA 3: DATA 4: DATA 6:

Data Types & Directives DATA 1: DATA 2: DATA 3: DATA 4: DATA 6: ORG 500 H DB 28 ; DECIMAL (1 C in Hex) DB 00110101 B ; BINARY (35 in Hex) DB 39 H ; HEX ORG 510 H DB “ 2591” ; ASCII NUMBERS ORG 518 H DB “My name is Joe” ; ASCII CHARACTERS

Access RAM Locations Using Register Names

Access RAM Locations Using Register Names

Access RAM Locations Using Addresses

Access RAM Locations Using Addresses

Switch Register Banks

Switch Register Banks

Pushing onto Stack

Pushing onto Stack

Popping from Stack

Popping from Stack

Stack & Bank 1 Conflict

Stack & Bank 1 Conflict

Stack & Bank 1 Conflict

Stack & Bank 1 Conflict

Arithmetic Instructions and Programs

Arithmetic Instructions and Programs

Outlines • Range of numbers in 8051 unsigned data • Addition & subtraction instructions

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

Addition of Unsigned Numbers • ADD A, source ; A = A + source

Addition of Unsigned Numbers • ADD A, source ; A = A + source

Addition of Individual Bytes

Addition of Individual Bytes

ADDC & Addition of 16 -bit Numbers + 1 3 C 3 B 78

ADDC & Addition of 16 -bit Numbers + 1 3 C 3 B 78 E 7 8 D 74

BCD Number System • Unpacked BCD: 1 byte • Packed BCD: 4 bits

BCD Number System • Unpacked BCD: 1 byte • Packed BCD: 4 bits

Adding BCD Numbers & DA Instruction MOV ADD DA + + A, #17 H

Adding BCD Numbers & DA Instruction MOV ADD DA + + A, #17 H A, #28 H A, #47 H B, #25 H A, B A HEX 29 18 41 6 47 ; A=47 H first BCD operand ; B=25 second BCD operand ; hex (binary) addition (A=6 CH) ; adjust for BCD addition (A=72 H) BCD 0010 + 0001 0100 + 0100 1001 1000 0001 0110 0111 AC=1

Example

Example

Subtraction of Unsigned Numbers • SUBB A, source ; A = A – source

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

Example (Positive Result)

Example (Positive Result)

Example (Negative Result)

Example (Negative Result)

SUBB When CY = 1 • For multibyte numbers

SUBB When CY = 1 • For multibyte numbers

Multiplication of Unsigned Numbers • MUL ; A B, place 16 -bit result in

Multiplication of Unsigned Numbers • MUL ; A B, place 16 -bit result in B and A AB MOV MUL A, #25 H B, #65 H AB ; load 25 H to reg. A ; load 65 H in reg. B ; 25 H * 65 H = E 99 where ; B = 0 EH and A = 99 H Table 6 -1: Unsigned Multiplication Summary (MUL AB) Multiplication Operand 1 Operand 2 Result byte A B A=low byte, B=high byte

Division of Unsigned Numbers • DIV AB MOV DIV ; divide A by B

Division of Unsigned Numbers • DIV AB MOV DIV ; divide A by B A, #95 H B, #10 H AB ; load 95 into A ; load 10 into B ; now A = 09 (quotient) and ; B = 05 (remainder) Table 6 -2: Unsigned Division Summary (DIV AB) Division Numerator Denominator Quotient Remainder byte / byte A B B A

Example ( 1 of 2 )

Example ( 1 of 2 )

Example ( 2 of 2 )

Example ( 2 of 2 )

Signed 8 -bit Operands • Convert to 2’s complement – Write magnitude of number

Signed 8 -bit Operands • Convert to 2’s complement – Write magnitude of number in 8 -bit binary (no sign) – Invert each bit – Add 1 to it

Example

Example

Example

Example

Example

Example

Byte-sized Signed Numbers Ranges Decimal -128 -127 -126 …. -2 -1 0 +1 +2

Byte-sized Signed Numbers Ranges Decimal -128 -127 -126 …. -2 -1 0 +1 +2 … +127 Binary Hex 1000 0000 80 1000 0001 81 1000 0010 82 …………. . 1111 1110 FE 1111 FF 0000 00 0001 01 0000 0010 02 …………. . . 0111 1111 7 F

Overflow in Signed Number Operations

Overflow in Signed Number Operations

When Is the OV Flag Set? • Either: there is a carry from D

When Is the OV Flag Set? • Either: there is a carry from D 6 to D 7 but no carry out of D 7 (CY = 0) • Or: there is a carry from D 7 out (CY = 1) but no carry from D 6 to D 7

Example

Example

Example

Example

Example

Example