RAM equ $0 EEPROM equ $f800 STACK equ $00ff RESET equ $fffe SIZE equ 5 org RAM array fcb -4 fcb 4 fcb -5 fcb 7 fcb 3 answer rmb 1 org EEPROM start lds #STACK ldaa #SIZE psha ldx #array pshx jsr absum pula staa answer end jmp end ** * function absum (array, size) * array is passed by reference and is in the two bytes just under * the return address. * * size is call by value, and is in the byte just below array. * * array is the base address of an array with size components, * each of which is a 1-byte signed value * * returns the 1-byte sum of the absolute values of the components of array * * parameters are cleared off the stack * * the result is returned in the 1 byte just below the return address * * Registers affected: x, y, a, b * * Limitations: specified sum must fit into one byte * absum tsx ldy 2,x ldab 4,x aby dey * Y now contains the address of the last element of the array * This value, which we'll call "curaddress", will be used to step * backward through the array. clra * sum = 0 loop cpy 2,x blt doneas * if curaddress >= address of 1st array element ldab 0,y pshb jsr abs pulb aba * sum += absolute value of next array element dey * curaddress-- bra loop doneas staa 4,x * now get rid of the array parameter, making sure return address * is left at the top of the stack pulx puly pshx rts ** * function abs (N) * N is pass by value. It is a 1-byte signed integer, stored * just below the return address. * * returns the 1-byte absolute value of N * * parameters are cleared off the stack * * the result is returned in the 1 byte just below the return address * * Registers affected: none * * Limitations: none abs pshx psha pshb tsx * now N is at 6,x ldaa 6,x cmpa #0 bge enda ldab #-1 mul stab 6,x enda pulb pula pulx rts ORG RESET FDB start