Download presentation
Presentation is loading. Please wait.
1
Computer Science 210 Computer Organization
Introduction to Strings
2
Strings Sequences of characters, represented internally as ASCII values Basic ASCII is 8 bits, stored in lower order byte, with higher order byte clear A null character (ASCII 0) always terminates a string
3
;; Author: Ken Lambert ;; This program declares the string "Assembler is fun!" .ORIG x3000 ; Program code HALT ; Data variables MESSAGE .STRINGZ "Assembler is fun!" .END The STRINGZ directive puts a string’s ASCII values in consecutive memory cells, followed by a null character (ASCII 0)
4
String output with array-based loop and polling
;; Author: Ken Lambert ;; This program outputs the string "Assembler is fun!" ;; Pseudocode design: ; for ch in "Assembler is fun!" ; loop while display status >= 0 ; print ch .ORIG x3000 ;; Register usage: ; R1 = contents of display status register ; R0 = contents of display data register ; R2 = address of the next character in the string ; Main program code LEA R2, MESSAGE ; Get the base address of the string CHLOOP LDR R0, R2, #0 ; Get the next character from the string BRz ENDMAIN ; Quit when it's null (end of string) POLL LDI R1, DSR ; Poll for negative display status register BRzp POLL ; (ready bit = 1) STI R0, DDR ; Display is ready, so output the character ADD R2, R2, #1 ; Increment the character's address BR CHLOOP ENDMAIN HALT ; Main program data DSR .FILL xFE04 ; Address of the display status register DDR .FILL xFE06 ; Address of the display data register MESSAGE .STRINGZ "Assembler is fun!" .END String output with array-based loop and polling
5
String output with trap service routine PUTS
;; Author: Ken Lambert ;; This program outputs the string "Assembler is fun!" ;; Pseudocode design: ; print "Assembler is fun!" ;; Register usage: ; R0 = base address of the string .ORIG x3000 ; Main program code LEA R0, MESSAGE ; Load the address of the string PUTS ; Call the trap service routine to output it HALT ; Main program data MESSAGE .STRINGZ "Assembler is fun!" .END String output with trap service routine PUTS Using the trap service routine reduces 8 lines of code to 2
6
Watch Your Registers! The PUTS routine expects the address of the string in R0 Before the call, the return address (the incremented PC) is saved in R7
7
LC-3 Trap Service Routines
vector symbol routine x20 GETC read a single character (no echo) x21 OUT output a character to the monitor x22 PUTS write a string to the console x23 IN print prompt to console, read and echo character from keyboard x25 HALT halt the program The first four routines work with data in R0
8
More Problems Find the length of a string
Convert a string to uppercase or lowercase Convert a string of digits to an integer Convert an integer to a string of digits
9
For Wednesday String input
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.