Thread Closed

printing array of char to string in MIPS

 
Share Thread
Nov10-08, 10:31 PM   #1
 

printing array of char to string in MIPS


I am trying to mimic the function below in MIPS:

extern void print_int(int x);
extern void print_string(char x[]);

char y[1];

void main(void) {
char y[1];
y[0] = 'a';
y[1] = '\0';
print_string(y);
}


and my MIPS code for the above C code is below

can someone tell me why when I try to run this in spim it fails. It generates this error:

Memory address out of bounds

As far as I know I am already passing the address of y, which is an array of char terminated by a null string at the end. But why this error? How do I fix this?


Code:
.text
	.globl main
	main:
			# Function Entry
	sw $ra,-20($sp)
	sw $fp,-24($sp)
	add  $fp, $zero, $sp
	subu $sp, $sp, 24
			# MOVE 
	add $t0,$zero,0
	sw  $t0,-8($fp)
			# MOVE 
	add $t0,$zero,97
	lw  $t1,-8($fp)
	add  $t2,$zero,-2
	add $t1,$t2,$t1
	add $t1,$fp,$t1
	sb  $t0,($t1)
			# MOVE 
	add $t0,$zero,1
	sw  $t0,-12($fp)
			# MOVE 
	add $t0,$zero,0
	lw  $t1,-12($fp)
	add  $t2,$zero,-2
	add $t1,$t2,$t1
	add $t1,$fp,$t1
	sb  $t0,($t1)
			# MOVE 
	lw  $t0,-4($fp)
	add  $t1,$zero,-2
	add $t0,$t0,$t1
	add $t0,$fp,$t0
	lb  $t1,($t0)
	sw $t1,-16($fp)
			# PARAM
	lw  $t0,-16($fp)
	sub $sp,$sp,4
	sw $t0, ($sp)
			# PARAM in REG0
	lw  $a0,-16($fp)
			# PARAM in REG1
	lw  $a1,-16($fp)
			# Function call
	jal print_string
	add $sp,$sp,4
			# Function Ends
	LABEL1:
			# Exit Sequence
	lw $ra,-20($fp)
	add  $sp, $zero, $fp
	lw $fp,-24($fp)
	jal $ra
 
print_int:
	li $v0,1
	lw $a0, 0($sp)
	syscall
	jr $ra
 
print_string:
	li $v0,4
	lw $a0, 0($sp)
	syscall
	jr $ra
PhysOrg.com science news on PhysOrg.com

>> City-life changes blackbird personalities, study shows
>> Origins of 'The Hoff' crab revealed (w/ Video)
>> Older males make better fathers: Mature male beetles work harder, care less about female infidelity
Thread Closed

Similar discussions for: printing array of char to string in MIPS
Thread Forum Replies
char array initialisation problem Programming & Comp Sci 4
MIPS printing integer problem Programming & Comp Sci 0
Java: Int to String to Char Programming & Comp Sci 2
Matlab char array help please? Math & Science Software 2
C prog: printing values from array of structures Computing & Technology 36