char array in MIPS

Go To StackoverFlow.com

0

How would I create a char array and access those chars in MIPS? Im doing a project and part of it is to do this. I understand how to with integers and cant find any reference online on how to deal with just chars, specifically im trying to port...

static char   hexdigits[16] = "0123456789ABCDEF";

Heres my failed attempt:

hexarray: .word '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' #declare memory space for our hex array

EDIT: if someone could provide an example how to print out one of these items it would be very helpful (you can modify the code i have to whatever you wish). as I im just getting a memory address error.

2012-04-05 00:12
by jfisk
Maybe turn hexarray to a string using .asciiz instead of .word '0123456789ABCDEF' Then you could loop using lb and comparing their ascii values or whatever you need to do with it - Gohn67 2012-04-05 00:35
I am just trying to read an value hexarray[i] given a certain i (its in a loop - jfisk 2012-04-05 00:46
Yeah, if you aren't forced to use .word, then using an .asciiz string makes most sense to me. And you could get the value using lb - Gohn67 2012-04-05 00:50
I'm taking an Assembly course at the moment too, so I'm not too sure. You may need to use la (load address) and then lb (load byte), but I remember doing it without using load address before - Gohn67 2012-04-05 00:52


10

static char   hexdigits[16] = "0123456789ABCDEF";

Can be translated into:

.data
hexdigits: .byte '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'

Or

.data
hexdigits: .ascii "0123456789ABCDEF"

We can access the elements using

la    $t0, hexdigits
lb    $t1, 0($t0)        # $t1 = hexdigits[0]
lb    $t2, 1($t0)        # $t2 = hexdigits[1]

You can print the element using a system call (if your simulator support it. Most do)

la    $t0, hexdigits          # address of the first element
lb    $a0, 10($t0)            # hexdigits[10] (which is 'A')
li    $v0, 11                 # I will assume syscall 11 is printchar (most simulators support it)
syscall                       # issue a system call
2012-04-05 05:24
by Wiz
Ads