My first AI program seems to work, but there's one bit which seems "wrong". Unless I am simply being stupid...
But copy 256K block, I need to set the total_bytes 2 bytes higher for it to work, but why ?
Code: Select all
;Atari ST Assembly Program (Devpac compatible)
;Copies 256KB of TOS 2.06 from $E00000 to $4F00000 (79MB mark)
;Verifies copy using word accesses,reports failing address and data,waits for keypress
OPT W+ ;Enable warnings for debugging
SECTION TEXT
start:
;Print start message
lea start_msg(pc),a0
bsr print_string
;Initialize copy parameters
move.l #$E00000,source_addr ;TOS 2.06 start
move.l #$4F00000,dest_addr ;79MB mark
move.l #$40002,total_bytes ;256KB
;Comment out screen clear to avoid console issues
;move.w #2,-(sp) ;Console output
;move.w #$1B,d0 ;ESC code
;move.w #5,-(sp) ;Cconout
;trap #1
;addq.l #2,sp
;move.w #'E',d0 ;Clear screen
;move.w #5,-(sp)
;trap #1
;addq.l #2,sp
;Copy 256KB (word accesses)
move.l source_addr,a0
move.l dest_addr,a1
move.l total_bytes,d0
lsr.l #1,d0 ;Convert to words
subq.l #1,d0
copy_loop:
move.w (a0)+,(a1)+ ;Copy word
dbra d0,copy_loop
;Print copy complete
lea copy_done_msg(pc),a0
bsr print_string
;Verify copy (word accesses)
move.l #$E00000,a0 ;Source (TOS 2.06)
move.l #$4F00000,a1 ;Destination (79MB)
move.l total_bytes,d0
lsr.l #1,d0 ;Convert to words
subq.l #1,d0
verify_loop:
move.w (a0)+,d1 ;Source data
move.w (a1)+,d2 ;Destination data
cmp.w d1,d2
bne verify_fail
dbra d0,verify_loop
;Verification passed
lea verify_ok_msg(pc),a0
bsr print_string
bra.s wait_key
verify_fail:
;Report failing address and data
lea verify_fail_msg(pc),a0
bsr print_string
;Calculate failing address (destination base + offset)
move.l #$4F00000,a2 ;Destination base
move.l total_bytes,d3
lsr.l #1,d3 ;Total words
sub.l d0,d3 ;Words processed
subq.l #1,d3 ;Adjust for current
lsl.l #1,d3 ;Convert to bytes
add.l d3,a2 ;Failing address
;Print address
lea addr_msg(pc),a0
bsr print_string
move.l a2,d0
bsr print_hex_long
lea newline(pc),a0
bsr print_string
;Print source data
lea src_msg(pc),a0
bsr print_string
move.w d1,d0
bsr print_hex_word ;Print 16-bit data
lea newline(pc),a0
bsr print_string
;Print destination data
lea dst_msg(pc),a0
bsr print_string
move.w d2,d0
bsr print_hex_word ;Print 16-bit data
lea newline(pc),a0
bsr print_string
wait_key:
;Wait for keypress
lea keypress_msg(pc),a0
bsr print_string
move.w #1,-(sp) ;Cconin
trap #1
addq.l #2,sp
;Exit program
clr.w -(sp) ;Pterm0
trap #1
;Subroutine to print string
print_string:
move.l a0,-(sp) ;Push string address
move.w #9,-(sp) ;Cconws
trap #1
addq.l #6,sp
rts
;Subroutine to print longword as hex (8 digits)
print_hex_long:
move.l d0,-(sp) ;Save d0
lea hex_buffer(pc),a0
move.l d0,d1
moveq #7,d2 ;8 digits
hex_long_loop:
rol.l #4,d1 ;Get next nibble
move.b d1,d0
and.b #$0F,d0
cmp.b #10,d0
blt.s digit_long
add.b #'A'-10,d0
bra.s store_long
digit_long:
add.b #'0',d0
store_long:
move.b d0,(a0)+ ;Store in buffer
dbra d2,hex_long_loop
move.b #0,(a0) ;Null terminate
lea hex_buffer(pc),a0
bsr print_string
move.l (sp)+,d0 ;Restore d0
rts
;Subroutine to print word as hex (4 digits)
print_hex_word:
move.l d0,-(sp) ;Save d0
lea hex_buffer(pc),a0
move.w d0,d1
moveq #3,d2 ;4 digits
hex_word_loop:
rol.w #4,d1 ;Get next nibble
move.b d1,d0
and.b #$0F,d0
cmp.b #10,d0
blt.s digit_word
add.b #'A'-10,d0
bra.s store_word
digit_word:
add.b #'0',d0
store_word:
move.b d0,(a0)+ ;Store in buffer
dbra d2,hex_word_loop
move.b #0,(a0) ;Null terminate
lea hex_buffer(pc),a0
bsr print_string
move.l (sp)+,d0 ;Restore d0
rts
SECTION DATA
start_msg: dc.b "Copying TOS 2.06 to 79MB mark...",13,10,0
copy_done_msg: dc.b "Copy complete!",13,10,0
verify_ok_msg: dc.b "Verification passed!",13,10,0
verify_fail_msg: dc.b "Verification failed!",13,10,0
addr_msg: dc.b "Failing address: $",0
src_msg: dc.b "Source data: $",0
dst_msg: dc.b "Destination data: $",0
keypress_msg: dc.b "Press any key to exit...",13,10,0
newline: dc.b 13,10,0
hex_buffer: ds.b 9 ;8 digits + null
SECTION BSS
source_addr: ds.l 1
dest_addr: ds.l 1
total_bytes: ds.l 1
END