You are on page 1of 21

Assembly Language Programs

1) PROGRAM TO EXCHANGE TWO 16 BIT NUMBERS

Sl no. 1 2 3 4 5 6 7

ADDRESS 8000 8003 8004 8007 800A 800B 800E

HEX CODE 2A,00,F0 EB 2A,00,F1 22,00,F0 EB 22,00,F1 CF

LABEL

MNEMONICS COMMENTS LHLD F000 Load memory from specified locations. XCHG Exchange contents of HL & DE pairs. LHLD F100 Load memory from specified locations. SHLD F000 Store memory to specified locations. Exchange contents of HL & DE XCHG pairs. SHLD F200 Store memory to specified locations. Restart RST 1

INPUT (Before execution) F001 ; F000 F101 ; F100 12 ; AB 3C ; DE OUTPUT(After execution) F001 ; F000 F101 ; F100 3C ; DE 12 ; AB

K. Adisesha

1

Assembly Language Programs

2) PROGRAM TO ADD OR SUBTRACT TWO 8 BIT NUMBERS

Sl no. 1 2 3 4 5 6 7 8 9 10

ADDRESS 8000 8002 8004 8005 8008 800B 800D 8010 8012 8015

HEX CODE 3E, XX 06, YY 80/(90) 32,00,F0 D2,10,80 3E,01 C3,12,80 3E,00 32,01,F0 CF

LABEL

MNEMONICS COMMENTS MVI A, XX Move immediately to [A] the data XX MVI B, YY Move immediately to [B] the data YY ADD B/SUB B Add(or subtract) [B] with [A] Store contents of [A] in specified location JNC (Ahead 1) Jump on no carry to specified location Move immediately to [A] the MVIA, 01 data(01)H JMP(Ahead 2) Jump unconditionally ahead STA F000

Ahead 1 Ahead 2

MVIA, 00 STA F001 RST1

Move immediately to [A] the data(00)H Store contents of [A] in specified location Restart

INPUT : XX, YY 1) 2B, 1A 2) 2B, FA OUTPUT TRIAL ADDRESS 1 2

Addition (XX+YY) Subtraction (XX-YY) F001: F000 F001: F000 00 : 45 00 : 25 01 : 25 01 : 31

K. Adisesha

2

00.80 3C 32.10. 1 2 3 4 5 6 7 8 9 10 ADDRESS 8000 8001 8004 8005 8008 8009 800C 800F 8010 8013 HEX CODE AF 2A. 00.F2 CF LABEL MNEMONICS XRA A LHLD F000 XCHG LHLD F100 DAD D SHLD F200 JNC (Ahead) INR A COMMENTS Clear accumulator and Flag Load memory from specified locations Exchange contents of HL & DE pairs Load memory from specified locations Add HL & DE pairs Store memory to specified locations Jump on no carry to ahead address Increment contents of accumulator by one Store contents of [A] in specified location Restart Ahead STA F202 RST1 F001: F000 (X) 4A : 2C F101: F100 (Y) ED : 5B F202:F201: F200 (X+Y) 01 : 37 : 87 K. F0 EB 2A.F1 19 22.Assembly Language Programs 3) PROGRAM TO ADD TWO 16 BIT NUMBERS Sl no. Adisesha 3 .F2 D2.00.02.

L SUB E STA F200 MOV A.Assembly Language Programs 4) PROGRAM TO SUBTRACT TWO 16 BIT NUMBERS (X>Y) Sl no. Adisesha 4 . H SBB D STA F201 RST 1 COMMENTS Load memory from specified locations Exchange contents of HL & DE pairs Load memory from specified locations Move contents of [L] to [A] Subtract [E] from [A] Store result in specified location Move contents of [H] to [A] Subtract [D] from [A] with borrow Store result in specified location Restart F001: F000 (X) F101: F100 (Y) F202:F201: F200 (X-Y) FA : 2C BD : 5B 3C : D1 K.F1 7D 93 32. 1 2 3 4 5 6 7 8 9 10 ADDRESS 8000 8003 8004 8007 8008 8009 800C 800D 800E 8011 HEX CODE 2A.00.F2 7C 9A 32.00.00.F0 EB 2A.F2 CF LABEL MNEMONICS LHLD F000 XCHG LHLD F100 MOV A.01.

80 D2.80 3E.00 12 CF LABEL MNEMONICS LXIH F000 LXIB F100 LXID F200 MVI A. 01 JMP (Ahead 2) Ahead 1 Ahead 2 5 MVI A.01 C3.0B.F2 3E.26.00.81 AF 0A 8E 12 23 03 13 3A.00.F1 11. 00 STAX D RST 1 .80 3E.00. ADDRESS 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 8000 8003 8006 8009 800B 800E 800F 8010 8011 8012 8013 8014 8015 8018 8019 801C 801F 8021 8024 8026 8027 K.81 3D C2. (NN) COMMENTS Load HL pair with specified data Load BC pair with specified data Load DE pair with specified data Specify the number of bytes Save the count Clear accumulator and flags Load [A] indirectly from address specified by BC pair Add memory to [A] with carry Store contents of [A] indirectly in location specified by DE pair Update memory Update BC register pair Update DE register pair Load [A] from specified location Decrement count Jump on no zero to perform loop Jump on no carry to ahead address Move immediately to [A] the data(01)H Jump unconditionally to ahead address Move immediately to [A] the data(00)H Store contents of [A] indirectly in address specified by DE pair Restart LOOP STA 8100 XRA A LDAX B ADC M STAX D INX H INX B INX D LDA 8100 DCR A JNZ (LOOP) JNC (Ahead 1) MVI A. Adisesha HEX CODE 21.00.Assembly Language Programs 5) PROGRAM TO ADD TWO N BYTE NUMBERS Sl no.(NN) 32.F0 01.00.24.

Assembly Language Programs INPUT(X) INPUT(Y) OUTPUT(X+Y) F103 : F102 : F101 : F100 F003 : F002 : F001 : F000 F204:F203 : F202 : F201 : F200 A4 : E6 : F3 : D7 C3 : 54 : A2 : 1B 01: 68 : 3B: 95: F2 K. Adisesha 6 .

81 01.M STAX B INX H INX B DCR D JNZ (LOOP) RST1 INPUT 8100:810:8102:8103:8104:8105:8106:8107:8108:8109:810A:810B:810C:810D:810E:810F 00 : 01: 02 : 03 : 04 : 05 : 06 : 07 : 08 : 09 : 0A : 0B : 0C : 0D : 0E : 0F OUTPUT 8200:8201:8202:8203:8204:8205:8206:8207:8208:8209:820A:820B:820C:820D:820E:820F 00 : 01: 02 : 03 : 04 : 05 : 06 : 07 : 08 : 09 : 0A : 0B : 0C : 0D : 0E : 0F K. (10)H LXIH 8100 LXIB 8200 COMMENTS Set count in [D] Load HL pair with specified address Load BC pair with specified address Move contents of memory to [A] Store contents of [A] indirectly to location specified by BC pair Update memory Update BC pair Decrement count in [D] Jump on no zero to perform the loop Restart LOOP MOV A.00.10 21.00. 1 2 3 4 5 6 7 8 9 10 ADDRESS 8000 8002 8005 8008 8009 800A 800B 800C 800D 8010 HEX CODE 16.Assembly Language Programs 6) PROGRAM FOR BLOCK TRANSFER Sl no.80 CF LABEL MNEMONICS MVI D. Adisesha 7 .08.82 7E 02 23 03 15 C2.

08.Assembly Language Programs 7) PROGRAM TO BLOCK TRANSFER IN REVERSE ORDER Sl no. 1 2 3 4 5 6 7 8 9 10 ADDRESS 8000 8002 8005 8008 8009 800A 800B 800C 800D 8010 HEX CODE 16.80 CF LABEL MNEMONICS MVI D. (10)H LXIH 8100 LXIB 820F COMMENTS Set count in [D] Load HL pair with specified address Load BC pair with specified address Move contents of memory to [A] Store contents of [A] indirectly to location specified by BC pair Update memory Decrement BC pair by one Decrement count in [D] Jump on no zero to perform the loop Restart LOOP MOV A.00.10 21.M STAX B INX H DCX B DCR D JNZ (LOOP) RST1 INPUT 8100:810:8102:8103:8104:8105:8106:8107:8108:8109:810A:810B:810C:810D:810E:810F 00 : 01: 02 : 03 : 04 : 05 : 06 : 07 : 08 : 09 : 0A : 0B : 0C : 0D : 0E : 0F OUTPUT 820F:820E:820D:820C:820B:820A:8209:8208:8207:8206:8205:8204:8203:8202:8201:8200 0F : 0E : 0D : 0C : 0B : 0A : 09 : 08 : 07 : 06 : 05 : 04 : 03 : 02 : 01 : 00 K.81 01.0F.82 7E 02 23 0B 15 C2. Adisesha 8 .

D SHLD F200 RST 1 Clear accumulator and flags Set N count in [C] Clear [D] Load HL pair with specified data Add memory to [A] Decimal adjust accumulator Jump on no carry to ahead address Increment [D] by one Update memory Decrement count Jump on no zero to perform loop Move [A] to [L] Move [D] to [H] Store contents of HL pair to specified consecutive locations Restart K.08.00 21.A MOV H.0E.81 86 27 D2. Adisesha 9 .00.80 6F 62 22.80 14 23 0D C2.0A 16.F2 CF Ahead LOOP LABEL MNEMONICS COMMENTS XRA A MVI C.00.Assembly Language Programs 8) PROGRAM TO ADD N DECIMAL NUMBERS (N= 10) INPUT OUTPUT 8100:810:8102:8103:8104:8105:8106:8107:8108:8109 F201 F200 00 : 01: 02 : 03 : 04 : 05 : 06 : 07 : 08 : 09 00 45 Sl no. 00 LXIH 8100 ADD M DAA JNC(Ahead) INR D INX H DCR C JNZ(LOOP) MOV L. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ADDRESS 8000 8001 8003 8005 8008 8009 800A 800D 800E 800F 8010 8013 8014 8015 8018 HEX CODE AF 0E. (0A)H MVI D .

BE. K.06 11.F9.5 s using monitor delay subroutine Pop[A] and flags off stack Increment contents of [A] by one Perform the loop until [A] becomes zero Halt LOOP OUTPUT: ‘00’ up to ‘FF’ is displayed in data field.80 76 LABEL MNEMONICS XRA A STA FFF9 PUSH PSW CALL(UPDDT) LXID FFFF CALL(DELAY) POP PSW INR A JNZ (LOOP) HLT COMMENTS Clear accumulator and flags Store contents of accumulator in specified location Push [A] and flags to stack Call display subroutine for data field Implement a delay of 0.FF F5 CD.D3.04 F1 3C C2.FF.Assembly Language Programs 9) PROGRAM FOR ADDITION OF HEXA-DECIMAL NUMBERS UNTILL ‘FF’ IS ENCOUNTERED Sl no. Adisesha 10 .FF CD. 1 2 3 4 5 6 7 8 9 10 ADDRESS 8000 8001 8004 8005 8008 800B 800E 800F 8010 8013 HEX CODE AF 32.01.

00.00.81 0A 8E 27 12 23 03 13 3A. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ADDRESS 8000 8003 8006 8009 800B 800E 800F 8010 8011 8012 8013 8014 8015 8018 8019 801C 801F 8021 8024 8026 8027 HEX CODE 21.81 3D C2.0B.24.00.80 3E. (02) COMMENTS Load HL pair with specified data Load BC pair with specified data Load DE pair with specified data Specify the number of bytes Save the count Load [A] indirectly from address specified by BC pair Add memory to [A] with carry Decimal adjust accumulator Store contents of [A] indirectly in location specified by DE pair Update memory Update BC register pair Update DE register pair Load [A] from specified location Decrement count Jump on no zero to perform loop Jump on no carry to ahead address Move immediately to [A] the data(01)H Jump unconditionally to ahead address Move immediately to [A] the data(00)H Store contents of [A] indirectly in address specified by DE pair Restart LOOP STA 8100 LDAX B ADC M DAA STAX D INX H INX B INX D LDA 8100 DCR A JNZ (LOOP) JNC (Ahead 1) MVI A.26. 01 JMP (Ahead 2) Ahead 1 Ahead 2 MVI A.F1 11.00 12 CF LABEL MNEMONICS LXIH F000 LXIB F100 LXID F200 MVI A. Adisesha 11 .F2 3E.80 D2.01 C3.00.Assembly Language Programs 10) PROGRAM FOR FOUR DIGIT BCD ADDITION Sl no.00.02 32.80 3E. 00 STAX D RST 1 K.F0 01.

Adisesha 12 .Assembly Language Programs INPUT(X) INPUT(Y) OUTPUT(X+Y) F101 : F100 F001 : F000 F202 : F201 : F200 57 : 93 64 : 21 01: 22 : 14 K.

01/(02) 11.80 23 C3. 13.01/(02) Move to [B] (01) for 8-bit & (02) for 16-bit complement of data Load DE pair with specified data LXID 0001 LXIH F000 MOV A.08.F0 7E 2F 77 05 CA.F1 CF Ahead LOOP MVI B. ADDRESS HEX CODE LABEL MNEMONICS COMMENTS 1 2 3 4 5 6 7 8 9 10 11 12 13 14 8000 8002 8005 8008 8009 800A 800B 800C 800F 8010 8013 8016 8017 801A 06.80 2A.M CMA MOV M.Assembly Language Programs 11) PROGRAM TO FIND 2`S COMPLEMENT OF 8 BIT/ 16 BIT NUMBER Sl no.A DCR B JZ (Ahead) INX H JMP(Loop) LHLD F000 DAD D SHLD F100 RST 1 Load HL pair with specified data Copy contents of memory to [A] Complement contents of [A] Copy contents of [A] to memory Decrement [B] by one Jump on zero to ahead address Update memory Jump unconditionally to perform loop Load contents of HL pair from specified locations Add HL and DE pair of registers Store contents of HL pair to specified locations Restart DATA INPUT OUTPUT(2`s complement) K.F0 19 22. Adisesha 13 .01.00.00 21.00.00.

A MOV E.00.80 67 22.B CALL(Sub-BCD) MOV H.99 SUB E INR A ADD C DAA RET COMMENTS Load DE pair with specified data Load BC pair with specified data Call subroutine for BCD subtraction Copy contents of [A] to [L] Copy contents of [D] to [E] Copy contents of [B] to [C] Call subroutine for BCD subtraction Copy contents of [A] to [H] Store contents of HL pair to specified locations Restart Move to [A] the given data to subtract using 99` complement Subtract [E] from [A] Increment [A] by 1 Add [C] to [A] Decimal adjust accumulator Return from subroutine unconditionally Sub-BCD K.A SHLD F000 RST 1 MVI A.86. ADDRESS 8000 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 8003 8006 8009 800C 800D 800E 800F 8012 8013 8020 8017 8018 8019 801A 801B HEX CODE 11.20.20.99 93 3C 81 27 C9 LABEL MNEMONICS LXID(9786) LXIB(3259) CALL(Sub-BCD) MOV L.32 CD. Adisesha 14 .Assembly Language Programs 8-Bit F000 : 7A F100 : 87 16-Bit F001 : F000 :: 1A: 26 F101 : F100 :: E5: DA 12) PROGRAM FOR SUB TRACTION OF TWO 16-BIT BCD NUMBERS (X>Y) Sl no.D MOV C.F0 CF 3E.59.97 01. 80 6F 5A 48 CD.

00.(10)H COMMENTS Load HL pair with specified address Load HL pair with specified address Set counter in [C] for N counts Load [A] indirectly from address indicated by DE pair Copy contents of memory to[B] Copy contents of [A] to memory Copy contents of [B] to [A] Store contents of [A] indirectly onto address in DE pair Update memory Update DE register pair Decrement [C] by 1 Perform the loop until [C] becomes zero Restart LOOP LDAX D MOV B. Adisesha 15 .A MOV A. 1 2 3 4 5 6 7 8 9 10 11 12 13 ADDRESS 8000 8003 8006 8008 8009 800A 800B 800C 800D 800E 800F 8010 8013 HEX CODE 21.10 1A 46 77 78 12 23 13 0D C2.F1 0E.08.Assembly Language Programs OUTPUT F001: F000 :: 65 : 27 13) PROGRAM FOR BLOCK EXCHANGE Sl no.F0 11.80 CF LABEL MNEMONICS LXIH F000 LXID F100 MVI C.M MOV M.00.B STAX D INX H INX D DCR C JNZ(Loop) RST 1 INPUT F000:F001:F002:F003:F004:F005:F006:F007:F008:F009:F00A:F00B:F00C:F00D:F00E:F00F 00 : 01 : 02 : 03 : 04 : 05 : 06 : 07 : 08 : 09 : 0A : 0B : 0C : 0D : 0E : 0F F100:F101:F102:F103:F104:F105:F106:F107:F108:F109:F10A:F10B:F10C:F10D:F10E:F10F K.

Adisesha 16 .Assembly Language Programs 10 : 11 : 12 : 13 : 14 : 15 : 16 : 17 : 18 : 19 : 1A : 1B : 1C : 1D : 1E : 1F F000:F001:F002:F003:F004:F005:F006:F007:F008:F009:F00A:F00B:F00C:F00D:F00E:F00F 10 : 11 : 12 : 13 : 14 : 15 : 16 : 17 : 18 : 19 : 1A : 1B : 1C : 1D : 1E : 1F F100:F101:F102:F103:F104:F105:F106:F107:F108:F109:F10A:F10B:F10C:F10D:F10E:F10F 00 : 01 : 02 : 03 : 04 : 05 : 06 : 07 : 08 : 09 : 0A : 0B : 0C : 0D : 0E : 0F OUTPUT K.

Assembly Language Programs 14) PROGRAM FOR SORTING AN ARRAY OF ‘N-NUMBERS’ IN ASCENDING ORDER K. Adisesha 17 .

00.A DCX H MOV M.80 CF LABE L MNEMONICS XRA A MVI B.B DCR C LXIH F100 LOOP MOV A.80 05 C2.0B 48 0D 21.03.M MOV M.M INX H CMP M JC (AHEAD) MOV D.08.13. Adisesha 18 .Assembly Language Programs INPUT F100:F101:F102:F103:F104:F105:F106:F107:F108:F109 0A : 01 : 09 : 05 : 03 : 06 : 08 : 07 : 04 : 02 Sl no. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ADDRESS 8000 8001 8003 8004 8005 8008 8009 800A 800B 800E 800F 8010 8011 8012 8013 8014 8017 8018 801B HEX CODE AF 06.F1 7E 23 BE DA.0B COMMENTS Clear accumulator & flags Move a count (N+1) in [B] Copy contents of [B] to [C] Decrement contents of [C] by one Load the specified address in HL pair Copy contents of [M] to [A] Update memory Compare memory with accumulator Jump on carry to ahead address Copy contents of [M] to [D] Copy contents of [A] to [M] Decrement HL pair by one Copy contents of [D] to [M] Increment contents of HL pair by one Decrement contents of [C] by one Jump on no zero to perform the loop Decrement count in [B] Jump on no zero to specified address Restart START MOV C.80 56 77 2B 72 23 0D C2.D INX H AHEAD DCR C JNZ (LOOP) DCR B JNZ (START) RST 1 K.

Assembly Language Programs OUTPUT F100:F101:F102:F103:F104:F105:F106:F107:F108:F109 01 : 02 : 03 : 04 : 05 : 06 : 07 : 08 : 09 : 0A 15) PROGRAM TO CHECK 2-OUT-OF-5 CODE K. Adisesha 19 .

80 3E.00. ADDRESS 8000 8003 8005 8007 8009 800C 800F 8010 8013 8014 80015 8018 8019 801B 801E 8020 8023 8025 8028 INPUT F000: 11 F000: 13 ADDRESS HEX CODE 3A.05 16.80 3A.14. Adisesha 20 .Assembly Language Programs Sl no.F0 0F D2.23. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Sl no.00 ANI E0 JNZ (AHEAD 1) LDA F000 COMMENTS COMMENTS Load data into accumulator from specified location Move a count (05)H in [C] Clear contents of [D] Logical AND (E0) H with [A] Jump on no zero to specified address Load data into accumulator from specified location Rotate contents of [A] to right Jump on no carry to ahead address Increment count in [D] Decrement contents of [C] by one Jump on no zero to perform the loop Copy contents of [D] to [A] Compare (02) H immediately with [A] Jump on no zero to specified address Move ‘FF’ to [A] immediately Jump to specified location unconditionally Move ‘00’ to [A] immediately Store data from accumulator into specified location Restart LOOP RRC JNC(AHEAD 2) INR D AHEAD 2 DCR C JNZ(LOOP) MOV A. FF JMP(AHEAD 3) AHEAD 1 AHEAD 3 MVI A.25. 00 STA F100 RST 1 16) PROGRAM TO MULTIPLY TWO DIGIT BCD K.80 7A FE.00.E0 C2.00.00 32.80 3E.80 14 0D C2.F1 CF OUTPUT F100: FF F100: 00 HEX CODE LABEL LABEL MNEMONICS MNEMONICS LDA F000 MVI C.02 C2.F0 0E.FF C2.05 MVI D.D CPI .00 E6.0F.23.02 JNZ(AHEAD 1) MVI A.

A JNZ(START) SHLD(F100) RST 1 Move a count (0000)H in HL pair Load data into accumulator from specified location Copy contents of [A] to [B] Load data into accumulator from specified location Copy contents of [A] to [C] Copy contents of [C] to [A] Add [L] to accumulator Decimal adjust [A] Copy contents of [A] to [L] Clear contents of [A] Add contents of [H] to [A] Decimal adjust [A] Copy contents of [A] to [H] Copy contents of [B] to [A] Add immediately to accumulator 99 Decimal adjust [A] Copy contents of [A] to [B] Jump on no zero to specified address Store data from HL pair onto specified location Restart K. Adisesha 21 .A MOV A.A MVI A.Assembly Language Programs 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 8000 8003 8006 8007 800A 800B 800C 800D 800E 800F 80011 8012 8013 8014 8015 8017 8018 8019 801C 801F 21.80 22.A MOV A.00.00.C ADD L DAA MOV L.01.F1 CF INPUT F001: F000 11 : 12 OUTPUT F101: F100 01 : 32 START LXIH (0000 )H LDA F000 MOV B.F0 47 3A.A LDA F001 MOV C.0B.B ADI .99 DAA MOV B.99 27 47 C2.F0 4F 79 85 27 6F 3E.00 3A.00 8C 27 67 78 C6.00 ADC H DAA MOV H.00.