📄 chap4.asm
字号:
; Chapter 4 6808 assembly language programs; Jonathan W. Valvano; This software accompanies the book,; Real Time Embedded Systems published by Brooks Cole;; Program 4.1. This subroutine is nonreentrant because of the read-modify-write access to a global. Second rmb 1 Temporary global variable* Input parameters: Reg A,X contain 2 8 bit numbers* Output parameter: Reg A is returned with the averageAVE stx Second Save the second number in memory add Second Reg A=First+Second lsra (First+Second)/2 adc #0 round up? rts; Program 4.2. This subroutine is also nonreentrant because of the read-modify-write access to a global. Money rmb 1 bank balance implemented as a global* add 10 to the accountmore lda Money where Money is a global variable add #10 sta Money Money=Money+10 rts; Program 4.4. This assembly subroutine is nonreentrant because of the write-read access to a global. temp rmb 1 temporary result implemented as a global* calculate RegA=RegX+2*RegAmac stx temp Save X so that it can be added lsla RegA=2*RegA add temp RegA=RegX+2*RegA rts ; Program 4.6. This assembly subroutine is nonreentrant because of the multi-step write access to a global. Info rmb 2 16-bit data implemented as a global* set the variable using RegA and RegXset sta Info Info is a 16 bit global variable stx Info+1 rts; Program 4.8. This assembly subroutine is reentrant because it does not write to any globals. * Input parameters: Reg A,X contain 2 8 bit numbers* Output parameter: Reg A is returned with the averageAVE pshx Save the second number on the stack add 1,s Reg A=First+Second lsra (First+Second)/2 adc #0 round up? pulx rts; Program 4.9. This assembly subroutine is nonreentrant because of the read-modify-write access to a global. Status rmb 1 0 means empty, -1 means it contains somethingMessage rmb 1 data to be communicated* Input parameter: Reg X contains an 8 bit message* Output parameter: Reg CC (C bit) is 1 for OK, 0 for busy errorSEND tst Status check if mailbox is empty bmi Busy full, can't store, so return with C=0 stx Message store dec Status signify it is now contains a message sec stored OK, so return with C=1Busy rts; Program 4.10. This assembly subroutine is reentrant because it disables interrupts during the critical section. Status rmb 1 0 means empty, -1 means it contains somethingMessage rmb 1 data to be communicated* Input parameter: Reg X contains an 8 bit message* Output parameter: Reg CC (C bit) is 1 for OK, 0 for busy errorSEND clc Initialize carry=0 tpa save current interrupt state psha sei disable interrupts during vulnerable window tst Status check if mailbox is empty bmi Busy full, so return with C=0 stx Message store dec Status signify it is now contains a message pula ora #1 OK, so return with C=1 pshaBusy pula restore interrupt status tap rts; Program 4.12. This assembly subroutine can be used as part of a binary semaphore. * Global parameter: Semi4 is the memory location to test and set* If the location is zero, it will set it (make it -1)* and return Reg CC (Z bit) is 1 for OK* If the location is nonzero, it will return Reg CC (Z bit) = 0Semi4 fcb 0 Semaphore is initially freeTAS tst Semi4 check if already set bne Out busy, operation failed, so return with Z=0 dec Semi4 signify it is now busy bit #0 operation successful, so return with Z=1Out rts; Program 4.27. Example of a vectored interrupt. ; MC68HC708XL36TimeHan lda TSC ;read status sta TSC ;clear TOF;*Timer interrupt calculations* rtiExtHan lda ISCR ora #$40 ;ACK2 sta ISCR ;clear IRQ2;*External interrupt calculations* rti org $FFEC ;timer overflow fdb TimeHan org $FFE0 ;IRQ2/Keypad fdb ExtHan; Program 4.28. Example of a polled interrupt. ; MC68HC708XL36ExtHan brset 0,PORTD,PD0Han brset 1,PORTD,PD1Han bset 6,ISCR ;ACK2 rti ;no more PD0Han ;*PD0 interrupt calculations* bra ExtHan ;otherPD1Han ;*PD1 interrupt calculations* bra ExtHan ;other org $FFE0 ;IRQ2/Keypad fdb ExtHan; Program 4.29. Interrupting keyboard software. ; MC68HC708XL36; PD6-PD0 inputs = keyboard DATA; PD7=STROBE interrupt on riseInit sei ; make atomic clr DDRD ;all inputs mov #$80,KBICR ;rising PD7 mov #$40,ISCR ;arm,clr IRQ2 jsr InitFifo cli ;Enable IRQ rtsExtHan brset 7,PORTD,KeyHan swi ;errorKeyHan lda PORTD and #$7F jsr PutFifo bset 6,ISCR ;ACK2 rti org $FFE0 ;IRQ2/Keypad fdb ExtHan; Program 4.31. Helper routines for the printer interface. ; MC68HC708XL36;*****goes in RAM**************OK rmb 1 ;0=busy, 1=doneLine rmb 20 ;ASCII, end with 0Pt rmb 2 ;pointer to LinePt2 rmb 2 ;temporary;*****goes in ROM**************;Input RegH:X=>stringFill sthx Pt2 ;Pt2=>string ldhx #Line sthx Pt ;init pointerFloop ldhx Pt2 lda 0,X ;read data aix #1 sthx Pt2 ldhx Pt sta 0,X ;write data aix #1 sthx Pt tst ;end? bne Floop ldhx #Line sthx Pt ;init pointer clr OK rts;Return RegA=dataGet ldhx Pt lda 0,X ;return RegA=data aix #1 sthx Pt rts; Program 4.32. Initialization routines for the printer interface. ; MC68HC708XL36; PA6-PA0 outputs = printer DATA; PD0=START; PD1=READY interrupt on rise;Input RegX=>string;Input RegH:X=>stringInit sei ; make atomic bsr Fill ;Init global mov #$01,DDRD ;PD0 output mov #$FF,DDRA ;PA outputs mov #$01,PORTB ;START=1 mov #$02,KBICR ;rising PD1 mov #$42,ISCR ;arm,clr IRQ2 bsr Get bsr Out ;start first cli ;Enable IRQ rts Out clr PORTD ;START=0 sta PORTA ;write DATA inc PORTD ;START=1 rts; Program 4.33. ISR routines for the printer interface. ; MC68HC708XL36ExtHan brset 1,PORTD,PrtHan swi ;errorPrtHan bset 6,ISCR ;ACK2 bsr Get tsta beq Disarm bsr Out ;start next bra Done Disarm mov #$22,ISCR ;disarm IRQ2 inc OK ;line completeDone rti org $FFE0 ;IRQ2/Keypad fdb ExtHan
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -