Search code examples
easy68k

Easy 68k error when trap command is called


When I try to display the contents of a register using trap task 17 I get some weird error. Here's my code:

*Equates section
program_start equ $1000   *Start Location of program
timesToAdd    equ 10     *Number to multiply numToMultiply by
numToMultiply equ 512    *Number to multiply through cumulative sum

ORG    program_start
START:                  ; first instruction of program

* Put program code here
    MOVE.L  #$00000000,D0  *Initially set value in D0 to 0
    MOVE.B  #timesToAdd,D2  *Store times left to add in D2

loop    CMP.B    #0,D2         *Check if we are finished adding
        BEQ  loop_end          *exit loop if we are done
        SUB.B #1,D2            *decrement timesToAdd by 1
        ADDI.L #numToMultiply,D0 *Add numToMultiply to value in D0
        BCC skipSet
        MOVE.B #1,D1           *Set D1 to 1 if carry bit is set 
skipSet BRA loop
loop_end     
     MOVE.L D0,D2
     MOVE.L #17,D0

     CMP.B #0,D1    *Check if there was a carry
     BEQ skipCarry
     LEA Carry,A1
     Trap #15       *Print Carry: with carry bit
skipCarry
     MOVE.L D2,D1
     LEA Product,A1
     Trap #15

     SIMHALT             ; halt simulator

Carry DC.B 'Carry: '
Product DC.B 'Product= '
    END    START        ; last line of source

When I run this, I get this output: Output

State of Registers before the trap call: Before Trap

Any help would be appreciated.


Solution

  • Your code horribly misuses trap. trap #15 invokes an operation indicated by d0, see the help manual. You were looking for trap 15 operation 3,

    "Display signed number in D1.L in decimal in smallest field. (see task 15 & 20)"

    At the time trap #15 is called, your D0 is $1400, where lower byte is 0x00, which is interpreted as

    "Display n characters of string at (A1), n is D1.W (stops on NULL or max 255) with CR, LF. (see task 13)"

    A1 at the time is equal to the address of the byte "Product".

    It is trying to interpret the number as a c style string, and gives you garbage as a result.

    Also, keep in mind that by the time you call trap, your d0 or d1/etc may have changed. ALWAYS try to keep d0 assignment as close to trap call to avoid weird things from happening. first prepare your information, then set d0, then call trap.

    That is mostly the only thing preventing it from working, but I reformatted it anyway in a way that i'm more comfortable with.

    ;Equates section
    program_start equ $1000       ; Start Location of program
    timesToAdd    equ 10          ; Number to multiply numToMultiply by
    numToMultiply equ 512         ; Number to multiply through cumulative sum
    
        org    program_start
    start:                        ; first instruction of program
        move.l  #$00000000,  D0   ; Initially set value in D0 to 0
        move.b  #timesToAdd, D2   ; Store times left to add in D2
    
    loop:   
        cmp.b  #0, d2             ; Check if we are finished adding
        beq    loop_end           ; exit loop if we are done
        sub.b  #1, d2             ; decrement timesToAdd by 1
        addi.l #numToMultiply, d0 ; Add numToMultiply to value in D0
        bcc    skipSet
        move.b #1, d1             ; Set D1 to 1 if carry bit is set 
    
    skipSet:
        bra loop
    
    loop_end:
        ; Check if there was a carry
        cmp.b #0, d1    
        beq   skipCarry
        lea   Carry, a1
    
        ; Print Carry: with carry bit
        move.l #17, d0
        move.l d0,  d2
        trap   #15       
    
    skipCarry:
        move.l d2, d1
        lea Product, a1
    
        move.l d0, d1
        move.l #3, d0
        trap #15
    
        simhalt
    
    Carry   dc.b 'Carry: '
    Product dc.b 'Product= '
        end start