Docsity

ASCII Character Processing Program Solution - Prof. David E. Pellett, Assignments of Basic Electronics

Toggle sidebar
bg1
Page 1 of 2Prog_Prob_Soln.a
Printed: Thursday, March 18, 2004 6:32:23 PM
; Program to read up to 100 ASCII characters, sum up any integers, change upper to lower case
; and store in msg.
; Note that numbers are in the range $30 - $39 and the number can be found by setting all
; but the 4 least significant bits to 0.
; Note that upper case letters are in the range $41 to $5A and lower case differ by having
; bit 6 set to 1 (where bit 1 is the least sig. bit). One can get the lower case character
; by "or-ing" the upper case character bit-by-bit with $20, or simply adding $20 since
; the bit is not set for the upper case character (no chance of "carry").
; Combined solution to excercises 1-3 of M68000 Programming Notes, 6/10/99
; -D. Pellett 3/18/04 (beta version)
xref getchar, strout, decin, decout, newline, stop
start: lea msg1,a0 ; ask for number of characters to read (100 max)
move.w #50,d0
jsr strout
jsr decin ; get the number (in d0)
jsr newline
cmp.w #100,d0 ; see if it fits
ble fits ; it fits - branch
move.w #100,nchar ; too big - set nchar = 100
lea msg2,a0 ; send message to input 100 max
move.w #46,d0
jsr strout
jsr newline
jmp readem
fits: move.w d0,nchar ; enter number in nchar
lea msg3,a0 ; send message to input characters
move.w #26,d0
jsr strout
jsr newline
readem: lea msg,a1 ; put address of msg in a1
move.w nchar,d1 ; number of bytes to read
clr d2 ; clear the register for the sum
jmp enter ; enter loop at end
loop: jsr getchar ; getchar puts the character in d0
jsr addch ; subroutine to test and add integers, change case
move.b d0,(a1)+ ; move the character to memory
enter: dbra d1,loop ; subtract 1 from d1 and see if done
; now output the information
lea msg4,a0
move.w #37,d0 ; output info message
jsr strout
jsr newline
lea msg,a0 ; set up for outputting character string
move.w nchar,d0
jsr strout ; output the string
jsr newline
lea msg5,a0 ; output sum message
move.w #31,d0
jsr strout
move.w d2,d0 ; output the sum
jsr decout
jsr newline
jsr stop ; end of program
pf3
pf4

Partial preview of the text

Download ASCII Character Processing Program Solution - Prof. David E. Pellett and more Assignments Basic Electronics in PDF only on Docsity!

Prog_Prob_Soln.a Page 1 of 2 Printed: Thursday, March 18, 2004 6:32:23 PM

; Program to read up to 100 ASCII characters, sum up any integers, change upper to lower case ; and store in msg. ; Note that numbers are in the range $30 - $39 and the number can be found by setting all ; but the 4 least significant bits to 0. ; Note that upper case letters are in the range $41 to $5A and lower case differ by having ; bit 6 set to 1 (where bit 1 is the least sig. bit). One can get the lower case character ; by "or-ing" the upper case character bit-by-bit with $20, or simply adding $20 since ; the bit is not set for the upper case character (no chance of "carry"). ; Combined solution to excercises 1-3 of M68000 Programming Notes, 6/10/ ; -D. Pellett 3/18/04 (beta version)

xref getchar, strout, decin, decout, newline, stop

start: lea msg1,a0 ; ask for number of characters to read (100 max) move.w #50,d jsr strout jsr decin ; get the number (in d0) jsr newline cmp.w #100,d0 ; see if it fits ble fits ; it fits - branch move.w #100,nchar ; too big - set nchar = 100 lea msg2,a0 ; send message to input 100 max move.w #46,d jsr strout jsr newline jmp readem fits: move.w d0,nchar ; enter number in nchar lea msg3,a0 ; send message to input characters move.w #26,d jsr strout jsr newline readem: lea msg,a1 ; put address of msg in a move.w nchar,d1 ; number of bytes to read clr d2 ; clear the register for the sum jmp enter ; enter loop at end loop: jsr getchar ; getchar puts the character in d jsr addch ; subroutine to test and add integers, change case move.b d0,(a1)+ ; move the character to memory enter: dbra d1,loop ; subtract 1 from d1 and see if done

; now output the information

lea msg4,a move.w #37,d0 ; output info message jsr strout jsr newline lea msg,a0 ; set up for outputting character string move.w nchar,d jsr strout ; output the string jsr newline lea msg5,a0 ; output sum message move.w #31,d jsr strout move.w d2,d0 ; output the sum jsr decout jsr newline jsr stop ; end of program

Prog_Prob_Soln.a Page 2 of 2 Printed: Thursday, March 18, 2004 6:32:23 PM

; Subroutine addch tests ASCII character in d ; If it is a number, adds it to the sum in d ; If it is an upper case letter, changes it to lower case ; Lower case letter or number is returned in d

addch: and.b #$7F,d0 ; mask off parity bit of character cmp.b #$30,d0 ; see if it is less than $ blt skip ; if so, skip cmp.b #$39,d0 ; see if it is greater than $ bgt skip ; if so, skip move.w d0,d3 ; is a number - put in d3 (don't change d0) and.w #$000F,d3 ; get the number by masking off upper bits add.w d3,d2 ; add to sum in d rts ; return from subroutine skip: cmp.b #$41,d0 ; see if it is less than $41 (A) blt skip1 ; if so, skip cmp.b #$5A,d0 ; see if it is greater than $5A (Z) bgt skip1 ; if so, skip add.w #$20,d0 ; upper case letter - convert to lower skip1: rts

data msg1: dc.b 'Enter the number of characters to read (100 max): ' msg2: dc.b 'Your entry was too big. Enter 100 characters: ' msg3: dc.b 'Now enter the characters: ' msg4: dc.b 'Here is the string (all lower case): ' msg5: dc.b 'The sum of numbers entered is: ' msg: ds.b 100 ; set aside 100 bytes of storage nchar: dc.w 20 ; number of characters to read even end