Вы находитесь на странице: 1из 4

This program stores a string from the keyboard in memory.

This program prints a string from memory to the monitor.


.orig x3000 .orig x3000
ld r4, cz lea r2, buffer ; Initialize buffer ptr
lea r2, buffer ; Init buffer pointer start ldr r0, r2, #0 ; Get char into r0
start ldi r1, kbsr ; See if a char is there brZ quit ; Terminate on null
A)____________ ; branch to check again A)___________ ; Send R0 to monitor
B)____________ ; get the character wait B)___________ ; Are we done?
str r0, r2, 0 ; Store it in buffer brZP wait
not r0, r0 add r2, r2, #1 ; Move buffer ptr over 1
add r0, r0, r4 ; Check for termination brNZP start
not r0, r0 quit halt
brZ quit dsr .fill xfe04
add r2, r2, #1 ; Increment buffer pointer ddr .fill xfe06
brNZP start ; Do it again! buffer .stringz "Hello, World!"
quit halt .end
cz .fill x001A ; C)__________
kbsr .fill xfe00
kbdr .fill xfe02
buffer .blkw 0x0100
.end
A. This broken program adds two numbers. B. This broken program adds two numbers and leaves the
third unchanged.
.orig x3000
.orig x3000
ld r0, a
ld r0, a
ld r1, b
ld r1, b
jsr add_nums
ld r2, c
halt
lea r3, dest
a .fill 3
br madd
b .fill 4
halt
buffer .blkw 0x0500
dest .blkw 1
; this subroutine adds two numbers (r0 and r1) and
; this subroutine adds two numbers (r0 and r1)
; stores the result in r7
; and stores the result in the memory address
addme add r7, r0, r1
; stored in r3
ret
madd add r2,r0,r1
.end
str r2,r3,0
jmp r7
a .fill 3
b .fill 4
c .fill 5
.end
FOR BOTH

Step 3:
Step 4:
(after fetch & decode)
The real indirect address is loaded into the
The address of the “indirect” address
MDR
loaded into the MAR [PC + offset]

READING THE KEYPRESS FROM THE KEYBOARD DATA REGISTER (KBDR)

LDI RO, KBDR_ADDR


KBDR_ADDR .FILL xFE02
Step 5: Note that before this instruction happened, a previous
The special address xFE02 is copied from instruction should have checked KBSR (Keyboard
the MDR into the MAR Status Register) and made sure bit 15 was 1. This
indicates that the keyboard data is ready to be read.

Step 6a: Step 6b:


The address control logic determines that xFE02 is a The value of KBDR is saved into the MDR, Step 7:
memory mapped IO address and sets the INMUX just as if it was a real memory access. The value of MDR is stored into the
value to the line for KBDR This resets bit 15 of KBSR to 0 destination register.

WRITING A CHARACTER TO THE MONITOR USING THE DISPLAY DATA REGISTER (DDR)
STI RO, DDR_ADDR
Note that before this instruction happened, a previous
DDR_ADDR .FILL xFE06
instruction should have checked DSR (Display Status
Step 5: Register) and made sure bit 15 was 1. This indicates
The special address xFE06 is copied from that the monitor is able to output another character.
the MDR into the MAR

Step 6: Step 7a:


The address logic determines that xFE06 is a memory Step 7b:
The data from the source register (in the
mapped IO address and sets the write enable on the The data from the MDR is stored in the
instruction) is copied into the MDR
DDR (and not in regular memory) DDR. This resets bit 15 of DSR to 0
Key Features of
Assembly Language
Functions

Call subroutines with the JSR(R) instructions.


They act like a branch, but also update R7
to hold the return address.

Use JSR when the subroutine is within a PC offset


from you. Otherwise load the address into a
register and use JSRR.

LD R2, sub_w_addr
JSRR R2 ; works even if the
; subrountine is far
; from its caller

sub_w_addr .FILL sub_write


.BLKW 1000

JSR sub_write ; works if the subroutine


.BLKW 3 ; is nearby its caller

; This subroutine writes a single


A good convention is that the
Subroutine begins ; character, placed in R0, to the caller understands that any
here ; monitor registers used for parameters or
sub_write ST R1, SaveR1 return values should be unused
at call time.

try_write LDI R1, DSR If you function uses any other


BRzp try_write registers (like R1 in this example),
it should save their values and
STI R0, DDR restore them before the
function ends
LD R1, SaveR1

RET

DSR .FILL xFE04


DDR .FILL xFE06
SaveR1 .BLKW 1

You can return from your subroutine


using the RET instruction, or just JMP
R7. Both mean the same thing.

Вам также может понравиться