Arithmetic Instructions:
; 8-bit addition MOV A,#01EH ADD A,#127 ; A MOV 30H,#0BBH ADD A,30H ; A MOV R0,#30H ADD A,@R0 ; A
= ? = ? = ?
Arithmetic Instructions:
; ; ; 16-bit addition 13EFH + 16FBH = ? immediate addition MOV A,#0EFH ADD A,#0FBH MOV 30H,A MOV A,#13H ADDC A,#16H MOV 31H,A
2
Define Data Segment Area
DSEG AT 30H num1: ds 2 ; 16-bit variable num2: ds 2 ; 16-bit variable CSEG AT 8100H MOV num1,#0EFH MOV num1+1,#13H MOV num2,#0FBH MOV num2+1,#16H
3
Define Data Segment Area
MOV A,num1 ADD A,num2 MOV num2,A MOV A,num1+1 ADDC A,num2+1 MOV num2+1,A ; ; num2 = ?
Define Data Segment Area
0030 0030 0032 8100 8100 8103 8106 8109 DSEG AT 30H num1: ds 2; 16-bit variable num2: ds 2; 16-bit variable CSEG AT 8100H 7530EF MOV num1,#0EFH 753113 MOV num1+1,#13H 7532FB MOV num2,#0FBH 753316 MOV num2+1,#16H
5
Multiple precision: 32-bit addition
DSEG AT 30H num1: ds 4 ; 32-bit variable num2: ds 4 ; 32-bit variable ; ; ; ; num1 num2 num2 num2 = 12345678H = 1EFEECDAH = num1+num2 and carry = ?
6
Multiple precision: 32-bit addition
MOV R7,#4 ; 8 x 4 = 32-bit CLR C ; clear carry MOV R0,#num1; pointer to num1 MOV R1,#num2; pointer to num2 MOV A,@R0 ADDC A,@R1 ; add with carry MOV @R1,A ; save result INC R0 ; next location INC R1 DJNZ R7,loop
7
loop:
Multiple precision: 32-bit subtraction
MOV R7,#4 ; 8 x 4 = 32-bit CLR C ; clear borrow MOV R0,#num1; pointer to num1 MOV R1,#num2; pointer to num2 MOV A,@R0 SUBB A,@R1 ; sub with borrow MOV @R1,A ; save result INC R0 ; next location INC R1 DJNZ R7,loop
8
loop:
Multiplication and Division
MOV A,#16 MOV B,#255 MUL AB ; result is saved in B:A MOV DPL,A MOV DPH,B CALL PINT16U ; print 16-bit ; result to screen
9
Multiplication and Division
MOV MOV DIV A,#255 B,#16 AB ; 255/16 = 15.9375
; A = Int[A/B] ; B = Mod[A/B] ; test print result CALL PINT8U MOV A,B CALL PINT8U; result to screen
10
Lets compute 255/16
255/16 = 15.9375 Can we print result like above with simple multiply and divide instructions? ; 255/16 -> A = 15 B = 15 ; 15x10/16 -> A = 9 B = 6 ; 6x10/16 -> A = 3 B = 12 ; 12x10/16 -> A = 7 B = 8...
11
BCD Arithmetic
BCD is 4-bit Sample decimal number coded with binary number. BCD number: 1234H 1991H ; see sample code MOV A,#19H ADD A,#21H ;binary addition
12
BCD Arithmetic
; A = 19H ; now put followed + 21H = 3AH DA A instruction ADD instruction
DA A ; the result will be 40H!
13
BCD Arithmetic
; ; ; ; ; ; ; ; BCD1 = 12345678H BCD2 = 12345678H BCD1 = BCD1+BCD2 result must be 24691356H suppose such both BCD number are stored at external memory location 9000H-9006H use edit to enter the number
14
BCD Arithmetic
DSEG AT 30H BCD1 DS 4 ; provide internal BCD2 DS 4 ; memory for two BCD CSEG COPY: MOV R7,#8 MOV R0,#BCD1 MOV DPTR,#9000H LOOP: MOVX A,@DPTR MOV @R0,A
15
BCD Arithmetic
INC DPTR INC R0 DJNZ R7,loop RET check location 30H-37H of the internal memory 30H = 78563412H 34H = 78563412H
16
; ; ; ;
BCD Arithmetic
MOV R7,#4 MOV R0,#BCD1 MOV R1,#BCD2 CLR C loop2: MOV A,@R0 ADDC A,@R1 DA A MOV @R1,A
17
BCD Arithmetic
INC R0 INC R1 DJNZ R7,loop2 RET run single step and study the result after instruction ADDC and DA A
; ; ;
18