2 Replies - 769 Views - Last Post: 14 July 2013 - 07:02 PM

#1 amyhill0926  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-July 13

Concatenation in Assembly Language

Posted 08 July 2013 - 01:46 PM

I am trying to figure out why string2 is not printing out on the line that I need to concatenate string1 & string2. I am getting the following output when attempting:

Hello
Goodbye
Hello


I am supplying my code to see if anyone has any idea as to what I am doing wrong as any help would be greatly appreciated! It looks like my issue is in strcatLoop2 but I do not know enough about assembly to know for sure.

	.org 0
COSTAT:	.equ 	0xfffff110		;printer status port
COUT:		.equ 	0xfffff114		;printer data port

main:		la	r31, stack

		la	r0, string1
		la	r6,  prtstr
		brl	r30, r6

		la	r0, newline
		la	r6, prtstr
		brl	r30, r6

		la	r0, string2
		la	r6, prtstr
		brl	r30, r6

		la	r0, newline
		la	r6, prtstr
		brl	r30, r6

		la	r0, string1
		la	r1, string2
		la	r2, string3
		la	r6, strcat
		brl	r30, r6

		la	r0, string3
		la	r6, prtstr
		brl	r30, r6

		la	r0, newline
		la	r6, prtstr
		brl	r30, r6

		stop

strcat:	la	r27, strcatLoop2	;need pointer to second loop as well
		la 	r28, strcatLoop
		la 	r29, strcatOut
		addi 	r18, r0, 0		;copy first arg
		addi	r19, r1, 0		;copy second arg
		addi 	r20, r2, 0		;copy third arg

strcatLoop:	ld 	r21, (r18)		;load char from string1
		brzr 	r27, r21		;if it was zero get to next loop
		st 	r20, (r21)		;store character to destination
		addi	r18, r18, 4		;increment string1 pointer
		addi 	r20, r20, 4		;increment destination pointer
		br	r28			;branch to strcatLoop and do another char

strcatLoop2:	ld 	r21, (r19)		;load character from string2
		brzr 	r29, r21		;branch to strcatOut if char was null
		st	r20, (r21)		;store character to destination
		addi 	r19, r19, 4		;increment string2 pointer
		addi 	r20, r20, 4		;increment destination pointer
		br 	r27			;branch to strcatLoop2 for next char

strcatOut:	br	r30			;branch for home

prtstr:	la	r29, wait
		la	r28, end
		la	r27, loop1
		addi	r18, r0, 0		;copy arg
		
loop1:	ld	r19, (r18)
		brzr	r28, r19		;done if loaded zero

wait:		ld	r20, COSTAT
		brpl	r29, r20		
		st	r19, COUT
		addi	r18, r18, 4
		br	r27

end:		br	r30

strlen:	lar	r29, Out
		lar	r28, Loop
		addi 	r18, r0, 0
		sub	r19, r19, r19

Loop:		ld	r20, 0(r18)
		brzr	r29, r20
		addi	r19, r19, 1
		addi	r18, r18, 4
		br	r28

Out:		addi r4, r19, 0
		br	r30

string1:	.dc	"Hello"
		.dc	0x00
string2:	.dc	"Goodbye"
		.dc	0x00
newline:	.dc	0x0a, 0x00

	.org 2000
string3:

	.org 10000
stack:


Edited by Dogstopper: :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Concatenation in Assembly Language

#2 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: Concatenation in Assembly Language

Posted 10 July 2013 - 05:15 AM

Looks like SRC assembly?
New thing to me. I found somethinh in the net about that...

Anyway:

The last line looks suspicious:

strcatLoop: ld  r21, (r18)      ;load char from string1
        brzr    r27, r21        ;if it was zero get to next loop
        st  r20, (r21)      ;store character to destination


Aren't you storing the pointer to the output at the location pointed to by the last read character?
(I guess the parenthesis mean indirection: Contents of r20 is stored at the location whose address is in r21)

Another thing:
In the "strcatOut" you should probably add a NUL-character at the end of the concatenated string?
Was This Post Helpful? 0
  • +
  • -

#3 amyhill0926  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-July 13

Re: Concatenation in Assembly Language

Posted 14 July 2013 - 07:02 PM

It is SRC. Sorry, guess I should have stated that. I finally was able to get it to work. Thanks for the reply!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1