A P S
A P S

Reputation: 35

How to convert all the lowercase letters to Uppercase letters in MIPS Assembly Language program

Excuse me, can someone help me on this? I need to convert all the lowercase letters such as this "Hello how are you?" to "HELLO HOW ARE YOU?",

Here is what I have so far:

.data
firsttyped: .asciiz ""
newline: .asciiz "\n"

.text
main:
    li $v0, 8
    li $a1, 20
    la $a0, firsttyped
    syscall

    li $v0, 4
    li $t0, 0

loop:
    lb $t1, firsttyped($t0)
    beq $t1, 0, exit
    sub $t1, $t1, 32
    sb $t1, firsttyped($t0)
    addi $t0, $t0, 1
    j loop


exit:
    li $v0, 4
    la $a0, firsttyped
    syscall

    li $v0, 10
    syscall

Can someone help me out on this please? I need to prevent that error the underlined e from coming.

By the way I do not understand psuedo codes.

Upvotes: 2

Views: 12432

Answers (3)

Captain Nemo
Captain Nemo

Reputation: 262

Did you try subtracting dec 32 from each letter? In the Ascii table, the dec value for char 'A' is the number 65, and the char 'a' is dec 97, so 'a' - 32 = 'A'.

If you need the code just ask and I will write it for you.

Kind regards

Upvotes: 3

rcgldr
rcgldr

Reputation: 28808

As an alternative, you could use a predfined translation table of 256 characters, indexed by input character value, containing the translated character value, most of which would be the same as the input character index, except for lower case to upper case convertion.

For example, xlat_table['A'] = 'A' (no change), while you'd also have xlat_tbl['a'] = 'A' (convert to upper case).

You could use another program to create the source code for the table or just create it manually.

Using a C example for portions of the actual table:

unsigned char xlat_table[256] = {
    0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,    // index 0x00
    0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,    // index 0x08
    ...
    0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47     // index 0x40
    ...
    0x60,0x41,0x42,0x43,0x44,0x45,0x46,0x47     // index 0x60
    ...
    0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff};   // index 0xf8

So xlat_table['A'] == xlat_table[0x41] == 'A', and xlat_table['a'] == xlat_table[0x61] == 'A'. All the values == the indexes, except for [0x61] through [0x7A] == 0x41 to 0x5A, to translate lower case to upper case and not affect other values.

My guess for mips

xlattbl .byte 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
        .byte 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
        ...

        la      %t3,xlatbl
        ;       $t0 has next byte to translate
        add     $t0,$t0,$t3
        lb      $t0,($t0)

Upvotes: 1

gusbro
gusbro

Reputation: 22585

You should:

  • reserve space for the input buffer
  • check whether each character is a lower case letter
  • skip the character if it's not a lower case letter
  • make it upper case if it was lower case

To do so you would: replace .asciiz "" from firsttyped with:

 firsttyped: .space 20

Add the following lines after beq $t1, 0, exit

blt $t1, 'a', not_lower
bgt $t1, 'z', not_lower

and add the label not_lower before addi $t0, $t0, 1:

not_lower:    

Upvotes: 4

Related Questions