Skip to main content

Posts

Arrange in ascending order

Write a program to sort given 10 numbers from memory location 2200H in the ascending order. MVI B , 09 : "Initialize counter" START : "LXI H, 2200H: Initialize memory pointer" MVI C , 09H : "Initialize counter 2" BACK : MOV A , M : "Get the number" INX H : "Increment memory pointer" CMP M : "Compare number with next number" JC SKIP : "If less, don’t interchange" JZ SKIP : "If equal, don’t interchange" MOV D , M MOV M , A DCX H MOV M , D INX H : "Interchange two numbers" SKIP : DCR C : "Decrement counter 2" JNZ BACK : "If not zero, repeat" DCR B : "Decrement counter 1" JNZ START HLT : "Terminate program execution"

Separate even numbers from given numbers

Separate even numbers from given numbers LXI H, 2500H        :  "Initialize memory pointer 1"    LXI D, 2600H        :  "Initialize memory pointer2"    MVI C, 32H          :  "Initialize counter"    BACK:MOV A, M       :  "Get the number"    ANI 01H             :  "Check for even number"    JNZ SKIP            :  "If ODD, don't store"    MOV A, M            :  "Get the number"    STAX D...

Find the square of given number

Find the square of given number LDA 2500H   :  "Get data in accumulator"    MOV L, A    :  "Get data in register L"    MVI H, 26H  :  "Get 26 in register H"    MOV A, M    :  "Square of data in accumulator"    STA 2501 H  :  "Store Square in 2501 H"    HLT :  "Stop"

Calculate the sum of series of odd numbers

Calculate the sum of series of odd numbers LDA 2500H   MOV C, A            :  "Initialize counter"    LXI H, 2501H        :  "Initialize pointer"    MVI E, 00           :  "Sum low = 0"    MOV D, E            :  "Sum high = 0"    BACK: MOV A, M      :  "Get the number"    ANI 01H             :  "Mask Bit 1 to Bit-7"    JZ SKIP             :  "Don't add if...

Calculate the sum of series of even numbers

Calculate the sum of series of even numbers LDA 2500H   MOV C, A            :  "Initialize counter"    MVI B, 00H          :  "sum = 0"    LXI H, 2501H        :  "Initialize pointer"    BACK: MOV A, M      :  "Get the number"    ANI 01H             :  "Mask Bit l to Bit7"    JNZ SKIP            :  "Don't add if number is ODD"    MOV A, B            :  "Get the su...

Find smaller of two numbers

Find smaller of two numbers LXI H, 2501H : "Address of first number in H-L pair" MOV A, M : "1stt number in accumulator" INX H : "Address of 2 nd number in H-L pair" CMP M : "compare 2 nd number with 1st number" JC AHEAD : "Yes, smaller number is in accumulator. Go to AHEAD" MOV A, M : "No, get 2 nd number in the accumulator" STA 2503 H : "Store smaller number in 2503H" HLT : "Stop"

Find larger of two numbers

Find larger of two numbers LXI H, 2501H : "Address of first number in H-L pair" MOV A, M : "1stt number in accumulator" INX H : "Address of 2 nd number in H-L pair" CMP M : "compare 2 nd number with 1 st number" JNC AHEAD : "No, larger is in accumulator. Go to AHEAD" MOV A, M : "Yes, get 2 nd number in the accumulator" STA 2503 H : "Store larger number in 2503H" HLT : "Stop"

Finding 2's complement of a number

Finding 2's complement of a number LDA 2501 H  :  "Get data in accumulator"    CMA         :  "Take its 1's complement"    ADI, 01 H   :  "Add one in the number"    STA 2502 H  :  "Store the result in 2502 H"    HLT         :  "Stop"  

Add contents of two memory locations

Add contents of two memory locations LXI H, 2500H    :  "HL Points 2500H"    MOV A, M        :  "Get first operand"    INX H           :  "HL Points 2501H"    ADD M           :  "Add second operand"    INX H           :  "HL Points 2502H"    MOV M, A        :  "Store the lower byte of result at 2502H"    MVI A, 00        :  "Initialize higher byte result with 00H"    ADC A      ...

Subtract two 16-bit numbers

Subtract two 16-bit numbers LHLD 2500H      :  "Get first 16-bit number in HL"    XCHG            :  "Save first 16-bit number in DE"    LHLD 2502H      :  "Get second 16-bit number in HL"    MOV A, E        :  "Get lower byte of the first number"    SUB L           :  "Subtract lower byte of the second number"    MOV L, A        :  "Store the result in L register"    MOV A, D        :  "Get...

Add two 16-bits numbers with DAD instruction

Add two 16-bits numbers with DAD instruction LHLD 2501H  :  "Get 1st 16-bit number"    XCHG        :  "Save 1st 16-bit number in DE"    LHLD 2503H  :  "Get 2nd 16-bit number in H-L"    DAD D       :  "Add DE and HL"    SHLD 2505H  :  "Store 16-bit result in memory locations 2505H and 2506H" .   HLT         :  "Stop"

Add two 16-bits number with ADD and ADC instruction

Add two 16-bits number with ADD and ADC instruction LHLD 2501H   :  "Get 1st 16-bit number in H-L pair"    XCHG         :  "Save 1st 16-bit number in DE"    LHLD 2503H   :  "Get 2nd 16-bit number in H-L pair"    MOV A, E     :  "Get lower byte of the 1st number"    ADD L        :  "Add lower byte of the 2nd number"    MOV L, A     :  "Store result in L-register"    MOV A, D     :  "Get higher byte of the 1st number"    ADC H    ...

Subtract two 8-bit numbers

Subtract two 8-bit numbers LXI H, 2501H   :   "Get address of first number in H-L pair. Now H-L points to 2501H"    MOV A, M       :   "Get first operand in accumulator"    INX H          :   "Increment content of H-L pair. Now, H-L points 2502H"    SUB M          :   "Subtract first to second operand"    INX H          :   "H-L points 4002H"    MOV M, A       :   "Store result at 2503H"    HLT    ...