Study Web

Week 9

Extended Array Example – lowercase conversion

Extended Array Example – · lowercase conversion

Extended Array Example – lowercase conversion

Array (and ASCII) example – convert to lowercase

  • Let’s write an ASM program that reads a string from input and converts all alphabetic characters to lower case
  • Algorithm:
  • Store input string in array
  • Iterate through the array and check each character
  • If character needs converting, convert to lowercase
  • If not, leave as is
  • Once complete, write string to output

Using ASCII to do conversion

  • Recall every character has an ASCII code (0 – 255)
  • Uppercase alphabetic characters are within the

Using ASCII to do conversion

  • Recall every character has an ASCII code (0 – 255)
  • Uppercase alphabetic characters are within the
  • Lowercase alphabetic characters are within the

Using ASCII to do conversion

  • Recall every character has an ASCII code (0 – 255)
  • Uppercase alphabetic characters
  • Lowercase alphabetic characters
  • Conversion of uppercase to lowercase requires subtracting 32 from ASCII value
  • E.g. “A” (65) -> (65 + 32) -> “a” (97)

Uppercase to lowercase

  • First we need to define an array to store the string:
  • We then need to read an input string into the array:
MOV R0, #stringData
STR R0, .ReadString
  • We now need to implement a loop that inspects each character.
  • Lets start with the basic loop structures first. We need:
  • A register to hold the array base address
  • A register to hold the index of the current character
  • A register to store the actual character value once
  • A condition to determine when we have finished scanning each character
  • We now need to implement a loop that inspects each character.
  • Lets start with the basic loop structures first. We need:
  • A Label to mark the start of the loop (so we can branch back) toLower:
  • A register to hold the array base address (we already have this in R0) MOV R0, #stringData
  • A register to hold the index of the current character (let’s use R1) MOV R1, #0 // initialise index to 0
  • A register to store the actual character value once (let’s use R2) LDRB R2, [R0 + R1] // reads the byte at address R0+R1
  • A condition to determine when we have finished scanning each character
  • we can look for the “NULL” character, which is always appended to the end of any input string.
  • A condition to determine when we have finished scanning each character
  • we can look for the “NULL” character, which is always appended to the end of any input string.
  • A condition to determine when we have finished scanning each character
  • we can look for the “NULL” character, which is always appended to the end of any input string.
  • So ….

CMP R2, #0

BNE toLower

Uppercase to lowercase

  • A condition to determine when we have finished scanning each character
  • we can look for the “NULL” character, which is always appended to the end of any input string.
  • So ….

CMP R2, #0

BNE toLower

This will keep iterating through the loop “toLower” until the NULL character is reached

Uppercase to lowercase – putting loop together

MOV R0, #stringData
STR R0, .ReadString
MOV R1, #0 //index

toLower: // start of loop

LDRB R2, [R0+R1] // read byte from array
ADD R1,R1,#1 // increment index
CMP R2, #0

BNE toLower

Uppercase to lowercase
conversion code
  • We now need to consider the conversion:
  • Only need to convert if read character is actually uppercase alphabetic
  • If character is uppercase alphabetic:
  • Add 32 to the value
  • Write the new value back to the array
  • If character is not uppercase alphabetic:
  • Leave as is (i.e. skip the above)
Uppercase to lowercase
conversion code
  • We now need to consider the conversion:
  • Only need to convert if read character is actually uppercase alphabetic
  • If character is uppercase alphabetic:
  • Add 32 to the value
  • Write the new value back to the array
  • If character is not uppercase alphabetic:
  • Leave as is (i.e. skip the above)

Determine if its uppercase alphabetic

toLower: // start of loop

LDRB R2, [R0+R1] // read byte from array

CMP R2, #65 // check lower value limit

BLT skipConversion

CMP R2, #90 // check upper value limit

BGT skipConversion

skipConversion: // jump to here if value not uppercase

ADD R1,R1,#1 // increment index
CMP R2, #0

BNE toLower

if it’s uppercase, convert to lowercase

toLower: // start of loop

LDRB R2, [R0+R1] // read byte from array
CMP R2, #65 // check lower value limit

BLT skipConversion

CMP R2, #90 // check upper value limit

BGT skipConversion

ADD R2,R2,#32 // convert to lowercase value
STRB R2, [R0+R1] // write it back to the array

skipConversion: // jump to here if value not uppercase

ADD R1,R1,#1 // increment index
CMP R2, #0

BNE toLower

All together

MOV R0, #stringData // store array base address
STR R0, .ReadString // read in input
MOV R1, #0 // initialise index

toLower: // start of loop

LDRB R2, [R0+R1] // read byte from array
CMP R2, #65 // check lower value limit

BLT skipConversion // if lower than uppercase range, skip conversion

CMP R2, #90 // check upper value limit

BGT skipConversion // if greater than uppercase range, skip conversion

ADD R2,R2,#32 // convert to lowercase value
STRB R2, [R0+R1] // write it back to the array

skipConversion: // branch destination if value was not uppercase

ADD R1,R1,#1 // increment index
CMP R2, #0 // check if NULL character

BNE toLower // Keep looping if not NULL character

STR R0, .WriteString // write converted string to output

HALT