For these problems, you only need to provide us with the subroutine (procedure) code itself (the assembly code, not the .s19 file). We'll hook up a main program that calls your procedure. Also, note that these subroutines won't require the full stack manipulation stuff we've talked about in class.
org $f800 main lds #$00ff ldaa #$70 jsr CAST eloop bra eloop *========== subroutine CAST ============= CAST tab *copy the number in A into B anda #$80 *test whether it is negative beq pos *if not, set A to 0 ldaa #$ff *otherwise, set A to $ff rts pos clra rts end main
org $f800
main lds #$00ff
ldaa #ff
ldab #ff
jsr abs
eloop bra eloop
*========== subroutine abs =============
**** algorithm: abs(x) = x if x is positive
**** = complement(x)+1 if x is negative
abs tsta *test whether D is negative or positive
bge ret *if positive, no change is needed
coma *complement A (Note! C bit gets set!)
comb *complement B (Note! C bit gets set!)
clc *clear the C bit
incb *add 1 to B (may set C bit ! )
adca #0 *if C bit set, add 1 to A
ret rts
end main
org $f800 table fdb 0,1,2,8,3,7,4,6,5,9 count fcb 10 main lds #$00ff ldx #table *pass the address of the table through stack pshx ldaa count *pass the number of elements in the table through stack psha jsr MAX *call the subroutine ins *restore the stack ins ins eloop bra eloop *========== subroutine MAX ============= MAX pshx *push X pshy *push Y tsx *get the address of the top element of the stack to X ldaa 6,x *get the number of elements in the table into A ldy 7,x *get the start address of the table ldx 0,y *get the first number in the array (as the initial maximum) loop tsta *test whether we have looked all the numbers beq ret *if so, then return the maximum in the tables cmpx 0,y *compare the current maximun with the next number in the table bhi same *if higher, keep the current maximum ldx 0,y *otherwise update it with current number in the table same iny *move on to the next number in the table iny deca *decrement the counter bra loop *keep looking the followinf numbers ret xgdx *get the maximum in X into D puly *pop Y pulx *pop X rts end main
00 in the A
accumulator if the condition is true, or ff if it is
false. The references to the tables will be passed on the stack; the
first item pushed will be the number of elements in the two tables
(this is a one-byte value); the second will be the address in memory
of the first table (this is a two-byte value); the third will be the
address in memory of the second table (this will also be a two-byte
value).
org $f800 tab1 fcb 1,2,3,4,5 tab2 fcb 2,1,2,3,4 count fcb 5 main lds #$00ff *initialize stack ldaa count *pass the number of elements in each table through stack psha ldx #tab1 *pass the start address of the first table through stack pshx ldx #tab2 *pass the start address of the second table through stack pshx jsr ALLGE *call the subroutine ins *restore the stack ins ins ins ins eloop bra eloop *============== subroutine ALLGE =============== ALLGE pshx *push X pshy *push Y pshb *push B tsx *get the address of the top element of the stack into X ldaa 11,x *get the number of elements in each table into A (as a counter) ldy 9,x *get the start address of the first table ldx 7,x *get the start address of the second table loop tsta *test whether we have looked all the pairs of numbers from the tables beq ret00 *if so, return 00 ldab 0,y *get the current number from the first table cmpb 0,x *compare this number to the paired number in the second table blt retff *if "less than", then return $ff deca *decrement the counter iny *move on to the next number of the first table inx *move on to the next number of the second table bra loop *keep looping ret00 ldaa #0 *prepare to return 0 through A bra restore retff ldaa #$ff *prepare to return #$ff through A restore pulb *restore the stack puly pulx rts end main