Click to See Complete Forum and Search --> : reading backwards


got1sleeve
October 2nd, 2008, 03:36 PM
hey everyone so im in class for assembly languge relatively new to the programming languge. We wrote this code previously to input the hello world message but now i need to read it backwards through a subroutine. I figured out how to read it backwards by changing the y+ to -y but trying that in the subroutine just gives me HHHH etc. also it overwrites the forward written "Hello world" Any help would be greatly appreciated. Im using avr studio 4 and a mega128 board for programming if that matters.

.include "m128def.inc"

;-------------------------------------------------------------------
;Reserve table space in Data Memory and tag location.
;This space is in SRAM. It is uninitialized at power up and must
;be populated by program execution.

.DSEG
.ORG 0x0110
string_table1: .BYTE 32

.ORG 0x0120
string_table2: .BYTE 32

.ORG 0x0130
string_table3: .BYTE 32

.ORG 0x0140
ascii_table: .BYTE 128

;-------------------------------------------------------------------
;Place string in Program Memory (Code Segment) and tag location.
;The string is placed in non-volatile memory by the compiler
;rather than by program execution at run time.

.CSEG
.ORG 0x0010

msg1: .DB "Hello, world! "

;-------------------------------------------------------------------
;Populate reset vector, jump to program start, and go.
;Note that .ORG can place code in memory in arbitrary sequence as
;long as reserved spaces are non-overlapping.

.CSEG
.ORG 0x0000

rjmp start

;-------------------------------------------------------------------
;Start main program beyond Interrupt Vector Table
;Initialize Stack Pointer

.ORG 0x0020
start:
ldi r16,low(RAMEND)
out SPL,r16
ldi r16,high(RAMEND)
out SPH,r16

;-------------------------------------------------------------------
;Initialize Data Memory: zeroes from start (0x0100) to end (0x10FF)

ldi ZL,low(0x0100)
ldi ZH,high(0x0100)
ldi r18,0x00
loop1:
st Z,r18
adiw Z,1
cpi ZL,0x00
brne loop1
cpi ZH,0x11
brne loop1

;-------------------------------------------------------------------
;Write printable ASCII characters (0x20 through 0x7F) to Data Memory
;The choice of data content is arbitrary. The writing is the point.
;
ldi ZL,low(ascii_table)
ldi ZH,high(ascii_table)
ldi r16,0x20
loop2:
st Z+,r16
inc r16
cpi r16,0x80
brne loop2

;-------------------------------------------------------------------
;Copy string from Program Memory to Data Memory.
;Note that Progam memory locations are referenced as words
;but program pointers (X, Y, Z registers) count bytes.
;Byte location in Program memory is word address x 2. See Z
;register manipulation below and 'st' instruction documentation.
;
ldi ZL,low(2*msg1)
ldi ZH,high(2*msg1)
ldi YL,low(string_table1)
ldi YH,high(string_table1)
ldi r16,16
loop3:
lpm r17,Z+
st Y+,r17
dec r16
brne loop3

;-------------------------------------------------------------------

ldi ZL,low(msg1)
ldi ZH,high(msg1)
ldi YL,low(string_table2)
ldi YH,high(string_table2)
ldi r20,16
rcall string_flip

;-------------------------------------------------------------------

;Trap Program Counter when done

loop4:
nop
rjmp loop4


string_flip:
lpm r17, Z+
st -Y, r17
dec r16
brne string_flip
ret