Docsity

String Manipulation: Character and Word Counting, String Insertion, and Data Conversion, Exams of Microcomputers

Partial preview of the text

Download String Manipulation: Character and Word Counting, String Insertion, and Data Conversion and more Exams Microcomputers in PDF only on Docsity!

ECE3120: Computer Systems

Chapter 4: Strings

Manjeera Jeedigunta

http://blogs.cae.tntech.edu/msjeedigun

Email: [email protected]

Tel: 931-372-6181, Prescott Hall 120

http://www.foxitsoftware.com For evaluation only.

o Prev…

n Indexable data structures

o Today…

n Strings

o Character and word counting

o String Insertion

o Data Conversion

o Introduction to subroutines

http://www.foxitsoftware.com For evaluation only.

Character and Word Counting

-A string is terminated by the NULL character.
-A new word is identified by skipping over the white space characters.
  • When a new word is identified, it must be scanned through before the next
word can be identified.

http://www.foxitsoftware.com For evaluation only.

Example 4.7 Write a program to count the number of characters and words
contained in a given string.
Solution:

tab equ $09 ;ASCII Code sp equ $ cr equ $0D lf equ $0A org $ char_cnt rmb 1 word_cnt rmb 1 string_x fcc "this is a strange test string to count chars and words." fcb 0 org $ ldx #string_x clr char_cnt clr word_cnt

string_lp ldab 1,x+ ; get one character and move string pointer lbeq done ; is this the end of the string? inc char_cnt

http://www.foxitsoftware.com For evaluation only.

insertion point

string

to be moved to the right

Step (^1) characters need to be movedfind out how many

2 count the # of characters to be inserted

make room for the substring to be inserted

3

4 insert the substring

Figure 4.6 Major steps of substring insertion

λ

λ

String Insertion

  • The pointers to the string and the substring to be inserted are given.
  • The insertion point is given.
  • The procedure is given in Figure 4.6.

http://www.foxitsoftware.com For evaluation only.

org $ ch_moved rmb 1 char_cnt rmb 1 sub_strg fcc "the first and most famous " fcb 0 string_x fcc "Yellowstone is national park." fcb 0 offset equ 15 ins_pos equ string_x+offset ; insertion point org $ ; the next 7 instructions count the number of characters to be moved ldaa # staa ch_moved ldx #ins_pos ; use x to point to the insertion point cnt_moved ldaa 1,x+ beq cnt_chars inc ch_moved bra cnt_moved cnt_chars dex ; subtract 1 from x so it points to the NULL character ldy #sub_strg ; use y as a pointer to the substring clr char_cnt

Example 4.9 Write a program to implement the string insertion algorithm.

http://www.foxitsoftware.com For evaluation only.

Word Matching

  • More detail flowchart is on Page 139

Start

Skip the white spaces to look for the next word in the string

Is the current word in the string equal to the word to be searched?

Is this the end of the string?

Stop

yes

no

no

yes

Figure 4P.8 Flowchart of the word search program

http://www.foxitsoftware.com For evaluation only.

tab equ $09 ; ASCII code of tab sp equ $20 ; ASCII code of space character cr equ $0D ; ASCII code of carriage return lf equ $0A ; ASCII code of line feed period equ $2E ; ASCII code of period comma equ $2C ; ASCII code of comma semicolon equ $3B ; ASCII code of semicolon exclamation equ $21 ; ASCII code of exclamation null equ $0 ; ASCII code of NULL character org $ match rmb 1 org $ clr match

Program to search for a given word

Initialization

http://www.foxitsoftware.com For evaluation only.

; the first nonwhite character is the beginning of a new word to be compared
ldy #word_x
ldaa 1,y+
next_ch cba
bne end_of_wd
cmpa #null ; check to see if the end of word is reached
beq matched ; "
ldaa 1,y+ ; get the next character from the word
ldab 1,x+ ; get the next character from the string
bra next_ch

http://www.foxitsoftware.com For evaluation only.

; the following 10 instructions check to see if the end of the given word is reached
end_of_wd cmpa #null
bne next_wd
cmpb #cr
beq matched
cmpb #lf
beq matched
cmpb #tab
beq matched
cmpb #sp
beq matched
cmpb #period
beq matched
cmpb #comma
beq matched
cmpb #semicolon
beq matched
cmpb #exclamation
beq matched

http://www.foxitsoftware.com For evaluation only.

Strings

-A sequence of characters terminated by a NULL (ASCII code 0)
-A number in the computer is represented as a binary number.
-Basic applications by manipulating strings
-Character or word counting
-String insertion
-Word matching
-Data Conversion

http://www.foxitsoftware.com For evaluation only.

Data Conversion

Example 4.4 Write a program to convert the unsigned 8-bit binary number in
accumulator A into BCD digits terminated by a NULL character. Each digit
is represented in ASCII code.
Solution:
  1. 8 bit numberà 0 to 255
  2. Include a null to terminate (say 255.)
  3. 4 bytes are needed to hold the converted BCD digits.
  4. Repeated division by 10 method is used to retrieve individual digits.
  5. Conversion to ASCII à add $30 to BCD digit

http://www.foxitsoftware.com For evaluation only.

; check to see if the number has two digits cmpb #99 ; is the number greater than 99 bhi three_dig ; if yes, the string has three digits ldx # idiv ;xàQ(remaining digit) , DàR (lower digit) addb #$30 ; convert the lower digit stab 1,y ; store the lowest digit xgdx addb #$ stab 0,y ; save the upper digit clr 2,y ; terminated the string with NULL bra done three_dig ldx # idiv addb #$ stab 2,y ; save the least significant digit xgdx ; swap the quotient to D(2 digits) ldx # idiv addb #$ stab 1,y ; save the middle digit xgdx ; swap the hundred’s digit to B (1 digit left) addb #$ stab 0,y ; save the ASCII code of the highest digit clr 3,y ; terminate the string with NULL done swi end

http://www.foxitsoftware.com For evaluation only.

Next…

o Subroutines

http://www.foxitsoftware.com For evaluation only.