profkelly wrote:
assemlbey_dude wrote:
Thanks for the reply. C uses \0 but this is treated a single character. I just tried modifying a program in the simulator $ff also worked. So, we don't have to use 0 to terminate a string do we.
A null terminated string must end with binary 0, $ff does not work as a string terminator.
Code:
ORG $1000
START: ; first instruction of program
move.b #14,d0
lea string,a1
trap #15
MOVE.B #9,D0
TRAP #15 ; halt simulator
* Variables and Strings
string dc.b '0123456789',$ff,'$ff did not work',0
END START ; last line of source
assemlbey_dude wrote:
In C we must use \0 correct?
Code:
string dc.b 'This is a 0string',0
Will not end before the s since every character(including spaces) inside the single quotation marks is treated as an ASCII character. The 0 in front of the s is written into memory as 48. The 0 after the comma is not converted to its eight bit ASCII representation but is rather converted to its hexadecimal value which is 00. Is all this correct?
Yes, your description of the EASy68K string is correct. In C a null terminated string is created by default when literal strings are used.
Code:
char str[] = "Creates null terminated string";
The null characters may be encoded using \0 or literal 0.
Code:
char str2[] = {'A','B','C','\0'};
char str3[] = {'D','E','F',0};
assemlbey_dude wrote:
What is the maximum number of elements a string can contain?
Code:
string dc.b 'This is a string',0
Using this same code I placed 266 letters in between the quotation marks and got an error. What is the max value of letters I can put. What effect does changing the .b to .w or .l have if any?
There is no limit on string length. The maximum length of any one line in the source is 256 characters. Just use multiple lines of source to create longer strings. The .B .W and .L syntax creates Byte, Word or Long word data. The data is padded with trailing zeros to make it fill the word or long word.
Code:
00001006= 61 62 63 64 65 14 str1 dc.b 'abcde'
0000100C= 6162 6364 6500 15 str2 dc.w 'abcde'
00001012= 61626364 65000000 16 str3 dc.l 'abcde'
Cleared up a lot for me. The only thing that is unclear to me is your code.
Code:
ORG $1000
START: ; first instruction of program
move.b #14,d0
lea string,a1
trap #15
MOVE.B #9,D0
TRAP #15 ; halt simulator
* Variables and Strings
string dc.b '0123456789',$ff,'$ff did not work',0
END START ; last line of source
I'm not familiar with traps. Why is the decimal number 14 moved into d0? Then you move the starting address of the string into a1 correct? What is trap #15? Why do you move the decimal number 9 into d0? Trap #15 once again here you wrote that it halts the simulator. Why do we need two of these traps? I see that only the second string was displayed. So this program just shows that the only way to terminate a string that is to be displayed is with a 0?
Here are three programs:
Code:
START ORG $1000
org $3000
string dc.b 'How many characters are there in this string?',0
number ds.b 1
org $1000
movea.w #string,a0
myloop move.b (a0)+,d1
cmp.b #0,d1
beq finished
add.b #1,d0
bra myloop
finished move.b d0,number
STOP #$2000
END START
This program counts how many characters there are in a string. The string is terminated with a zero. I believe this program has a limitation that it can only count up to 255 characters. I don't understand why, does it have something to do with .b? If there are more than 255 characters contained in the string it wraps around correct(Ie. 320 chars would have a length of 61 since it would count up to 255 and then start over)? If we want to be able to work with strings larger than 255 characters what do we need to change? All the .b to .w or .l ?
Is this line of code the limiting factor?
Code:
add.b #1,d0
The maximum number we can count up to is 255 using 8 bits. Since 2^8 is 256(0-255). So the string limits is 255 chars.
I changed the .b to .w and it seems to work now.
Code:
add.w #1,d0
Now the program can handle up to 2^16 65536 character strings?
As you can see below using this method I was able to use $FF as a string terminator. I suppose I could have used any 8 bit number correct?
Code:
START ORG $1000
org $3000
string dc.b 'How many characters are there in this string?',$ff
number ds.b 1
org $1000
movea.w #string,a0
myloop move.b (a0)+,d1
cmp.b #$ff,d1
beq finished
add.b #1,d0
bra myloop
finished move.b d0,number
STOP #$2000
END START
Is this how you are suppose to span a string across multiple lines of source code?
Code:
START ORG $1000
org $3000
string dc.b 'How many characters are there in this string?'
dc.b 'Lets try to continue this string down here',0
number ds.b 1
org $1000
movea.w #string,a0
myloop move.b (a0)+,d1
cmp.b #0,d1
beq finished
add.b #1,d0
bra myloop
finished move.b d0,number
STOP #$2000
END START
All this is still one string since the first line was not terminated, correct?