mips, c, and you

6
MIPS, C, and You

Upload: charles-vargas

Post on 02-Jan-2016

19 views

Category:

Documents


0 download

DESCRIPTION

MIPS, C, and You. Ropes. struct ropeNode { char * string; // 0x0 offset struct ropeNode * next; // 0x4 offset } typedef struct ropeNode* rope; rope weave(char * str, rope r){ // append str to the front of r (copy str) } void fray(rope r){ // free all memory associated with r }. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: MIPS, C, and You

MIPS, C, and You

Page 2: MIPS, C, and You

Ropesstruct ropeNode {

char * string; // 0x0 offset

struct ropeNode * next; // 0x4 offset

}

typedef struct ropeNode* rope;

rope weave(char * str, rope r){

// append str to the front of r (copy str)

}

void fray(rope r){

// free all memory associated with r

}

Page 3: MIPS, C, and You

Weave, C->MIPSstruct ropeNode {

char * string; // 0x0 offsetstruct ropeNode * next; // 0x4 offset

}rope weave(char * str, rope r){

rope end = (rope) malloc (sizeof(struct ropeNode));end->string = (char *) malloc ((strlen(str) + 1)*sizeof(char));strcpy(end->string, str);end->next = r;return end;

}

# $s0 = str, $s1 = r, $s2 = endweave:

# FILL ME IN!

Page 4: MIPS, C, and You

Weave, in MIPSweave:

#prologueaddiu $sp, $sp, -16sw $ra, 0($sp)sw $s0, 4($sp)sw $s1, 8($sp)sw $s2, 12($sp)#bodymove $s0, $a0

move $s1, $a1li $a0, 8

jal mallocmove $s2, $v0 move $a0, $s0jal strlen

addiu $a0, $v0, 1jal mallocsw $v0, 0($s2) move $a0, $v0move $a1, $s0jal strcpysw $s1, 4($s2)

move $v0, $s2#epiloguelw $ra, 0($sp)lw $s0, 4($sp)lw $s1, 8($sp)lw $s2, 12($sp)addiu $sp, $sp, 16jr $ra

Page 5: MIPS, C, and You

Fray, C->MIPS

struct ropeNode { char * string; // 0x0 offsetstruct ropeNode * next; // 0x4 offset

}

void fray(rope r){if (r->next != NULL)

fray(r->next);free(r->string);free(r);

}

fray:#fill me in!

Page 6: MIPS, C, and You

Fray, in MIPSfray:

addiu $sp, $sp, -8sw $ra, 0($sp)sw $s0, 4($sp)

move $s0, $a0lw $t0, 4($s0)beq $t0, $zero, donemove $a0, $t0jal fray

done: lw $a0, 0($s0)jal freemove $a0, $s0jal free

lw $ra, 0($sp)lw $s0, 4($sp)addiu $sp, $sp, 8jr $ra