Programming Examples Example 1 Write a program to

  • Slides: 8
Download presentation
Programming Examples

Programming Examples

Example 1: Write a program to store the data byte FFH in memory location

Example 1: Write a program to store the data byte FFH in memory location 9000 H MVI A, FFH ; STORE FFH IN THE ACCUMULATOR STA 9000 H ; COPY ACCUMULATOR CONTENTS TO THE ADDRESS 9000 H HLT ; STOP PROGRAM EXECUTION The following listing show the same function using indirect addressing LXI H, 9000 H ; LOAD HL EITH 9000 H MVI A, FFH ; STORE FFH IN THE ACCUMULATOR MOV M, A ; STORE FFH IN MEMORY LOCATION POINTED BY THE HL REGISTER PAIR HLT ; STOP PROGRAM EXECUTION

Example 2: Write a program to exchange the contents of memory location 9000 H

Example 2: Write a program to exchange the contents of memory location 9000 H and 9050 H LDA 9000 H ; Load the contents of memory location 9000 H in the accumulator MOV C, A ; Move the contents of the accumulator in register C LDA 9050 H ; Load the contents of memory location 9050 H in the accumulator STA 9000 H ; STORE THE CONTENTS OF THE ACCUMULATOR IN MEMORY ADDRESS 9000 H MOV A, C ; Move the saved contents in register C back to the accumulator STA 9050 H ; Store the contents of the accumulator in 9050 H HLT ; STOP PROGRAM EXECUTION

The following listing show the same function using indirect addressing LXI H, 9000 H

The following listing show the same function using indirect addressing LXI H, 9000 H ; Load the first address 9000 H in the HL register pair LXI D, 9050 H ; Load the second address 9050 H in the DE register pair MOV C, M ; Move the contents of memory location 9000 H to register C LDAX D ; Move the contents of memory location 9050 H to the accumulator MOV M, A ; Store the contents of the accumulator in memory location 9000 H MOV A, C ; Move the contents of register C to the accumulator STAX D ; Store the contents of the accumulator in memory location 9050 H HLT ; STOP PROGRAM EXECUTION

Example 3: Write a program to add the values presented in memory locations 9000

Example 3: Write a program to add the values presented in memory locations 9000 H and 9001 H, then store the results in memory location 9002 H LXI H, 9000 H ; Load the first address 9000 H in HL MOV A, M ; Load the first operand in the accumulator INX H ; Increment HL by 1 so it will point to the second memory location ADD M ; Add the contents of the memory location to the accumulator INX H ; Increment HL MOV M, A ; Store the contents of the accumulator in 9002 H HLT ; STOP PROGRAM EXECUTION

Example 4: Write a program to subtract the values presented in memory locations 9000

Example 4: Write a program to subtract the values presented in memory locations 9000 H and 9001 H, then store the results in memory location 9002 H Homework

Example 4: Write a program to add two 16 -bit values presented in memory

Example 4: Write a program to add two 16 -bit values presented in memory locations 9000 H and 9001 H, and 9002 H and 9003 H, then store the results in memory location 9004 H and 9005 H. Note the most significant bytes are 9001 H, 9003 H and 9005 H LHLD 9000 H XCHG LHLD 9002 H MOV A, E ADD L MOV L, A MOV A, D ADC H MOV H, A SHLD 9004 H HLT ; Load the first 16 bit value to HL ; Save the value of HL in DE ; Load the second 16 -bit value in HL ; Move the lower byte of the first value to A ; Add the lower bytes of the two numbers ; Store the result in L ; Move the higher byte of the first value to A ; Add the higher bytes with carry ; store the results in H ; store the results in memory location 9004 H ; Stop the program

LHLD 9000 H XCHG LHLD 9002 H DAD D SHLD 9004 H HLT ;

LHLD 9000 H XCHG LHLD 9002 H DAD D SHLD 9004 H HLT ; Load the first 16 bit value to HL ; Save the value of HL in DE ; Load the second 16 -bit value in HL ; Add the contents of DE to HL ; store the results in memory location 9004 H ; Stop the program