Sei sulla pagina 1di 8

Example 1 Write the instructions to add two decimal numbers, 50 and 72, and save the answer in register

C. MVI MVI ADD MOV HLT A, 50 B, 72 B C, A ; Load the number 50 into the accumulator ; Load the number 72 into register B ; Add the content of register B to the content ; of the accumulator. The sum is stored in the ; accumulator. ; Copy the sum from the accumulator into ; register C ; End of the program

COSC 243 Computer Architecture

Page 1

Instruction set examples

Example 2 Write the instructions to add two decimal numbers, 50 and 72, and save the answer in a memory location, called result. Use the assembler to name a constant, called cnst, for 50 and to use a memory location, called xyz, for 72. org cnst xyz: 0 equ db 50 72 1 a, cnst h, xyz m h, result M, A ; The code is to be loaded into memory ; beginning at location 0 ; Set cnst to 50 ; Allocate a memory location for 72, ; name it, and set it to 72 ; Allocate a memory location for result ; Load 50 into the accumulator ; Load the address of xyz into the H-L ; register pair ; Add the value, 72, to the value in the ; accumulator. The sum is stored in the ; accumulator. ; Load the address of result into the H-L ; register pair ; Copy the accumulator to the memory ; location, result ; Halt the processor ; End of assembly

result: ds start: mvi lxi add lxi mov hlt end start

COSC 243 Computer Architecture

Page 2

Instruction set examples

Example 3 Write the instructions to add two decimal numbers, 50 and 72 and save the answer in a memory location called, result. Use the assembler to name a constant, called cnst, for 50 and to use a memory location, called xyz, for 72. This time use LDA, ADI, STA. org cnst xyz: 0 equ db 50 72 1 xyz cnst result ; The code is to be loaded into memory ; beginning at location 0 ; Set cnst to 50 ; Set a memory location for 72, name it, and ; set it to 72 ; Set aside a memory location for result ; Load the accumulator with the value at ; memory location, xyz ; Add the value, 50, represented by cnst to ; the value in the accumulator. The sum is ; stored in the accumulator. ; Store the answer in memory location, result ; Halt the processor ; End of assembly

result: ds start: lda adi sta hlt end start

COSC 243 Computer Architecture

Page 3

Instruction set examples

Example 4 Write the instructions to continually read a value on an input port whose address is 100. If the number represented by the data on the input port is greater than 128, turn on a light attached to bit 7 of an output port whose address is 101. If the number is less than 128, turn off the light. Otherwise read the value again. org 0 100 101 128 A, 00H outport inport setpt loop on off ; The code is to be loaded into memory ; beginning at location 0 ; The input port is address 100 ; The output port is address 101 ; The set point is 128 ; Turn off the light to start. ; Read the input port into the accumulator ; Compare the value in the accumulator with ; the set point ; Continue the loop if the same ; Jump to on if the result is positive ; (input > 128) ; Jump to off if the result is negative ; (input < 128) ; Should never get here ; Make bit 7 of the accumulator a 1 to turn ; on the light ; Output the value to the output port ; Jump to loop to continue ; Make bit 7 of the accumulator a 0 to turn ; off the light ; Output the value to the output port ; Jump to loop to continue ; End of assembly
Page 4 Instruction set examples

inport equ outport equ setpt equ start: loop: mvi out in cpi jz jp jm hlt on: mvi out jmp off: mvi out jmp end start

A, 80H outport loop A, 00H outport loop

COSC 243 Computer Architecture

Example 5 Write the instructions to load 3 into the accumulator, decrement the accumulator to zero, and halt. org start: loop: 0 mvi dcr cpi jnz hlt end start A, 3 A 00H loop ; The code is to be loaded into memory ; beginning at location 0 ; Load the accumulator with 3 ; Decrement the accumulator by 1 ; Compare the value in the accumulator with ; zero ; Jump to on if the result is positive ; (acc > 0) ; Halt when the accumulator is zero ; End of assembly

COSC 243 Computer Architecture

Page 5

Instruction set examples

Example 6 Write the instructions to read a value on an input port whose address is 100. If the number represented by the data on the input port is 232, call a subroutine to add 3 to the value and store it in register E. org inport start: 0 equ lxi in cpi cz hlt sbrt: adi mov ret start 100 SP, 255 inport 232 sbrt 3 E, A ; The code is to be loaded into memory ; beginning at location 0 ; The input port is address 100 ; Set up the stack pointer ; Read the input port into the accumulator ; Compare the value in the accumulator with ; 232 ; Call the subrouting if (value = 232) ; Stop the program ; Add 3 to the value in the accumulator ; Copy the value to register E ; Return unconditionally to the program ; End of assembly

end

COSC 243 Computer Architecture

Page 6

Instruction set examples

Example 7 The following partial program stores some critical values in the accumulator, B, C, D, and E registers. It then calls a subroutine that is to read four values from memory locations called P, Q, R, and S. Sum the values. The result is stored in the memory location T. The subroutine then returns. It is important that after the subroutine returns, the values in the accululator, B, C, D, and E registers have not changed as a side-effect of the subroutine. You are to add the code required to make sure the registers in the main program are saved and restored in the subroutine. The locations of this code is indicated in the partial program. org 0 P: Q: R: S: T: start : db 20 db 21 db 22 db 23 ds 1 lxi mvi mvi mvi mvi mvi call hlt ; Load the code starting memory position 0 ; Allocate memory locations for P, Q, R, S ; and initialise them to some values ; Allocate a memory location for T ; Load the stack pointer with 255 ; Load A, B, C, D, E with critical values ; that we want to retain

SP, 255 A, 10 B, 11 C, 12 D, 13 E, 14 sbrt

; Call the subroutine ; Halt. Notice the original values were restored.

COSC 243 Computer Architecture

Page 7

Instruction set examples

sbrt : push A push B push D lda mov lda mov lda mov lda add add add sta P B, A Q C, A R D, A S B C D T ; Store A and PSW on the stack ; Store B and C on the stack ; Store D and E on the stack ; Put the value at location P into accumulator ; Move it to B ; Put the value at location Q into accumulator ; Move it to C ; Put the value at location R into accumulator ; Move it to D ; Put the value at location S into accumulator ; Notice our registers are different to what they ; were when we started the subroutine. ; Add the four values ; Store the result in T ; Restore D and E to the original values ; Restore B and C to the original values ; Restore A and PSW to the original values ; Return to calling program

pop D pop B pop A ret end start

; End of assembly

COSC 243 Computer Architecture

Page 8

Instruction set examples

Potrebbero piacerti anche