l04 - Procedures Let's try to make the code reusable... C...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Procedures Let's try to make the code reusable... C code int NumSpaces (char *s) { int count = 0; while (*s) { if (*s++ == ) count++; } /* count contains the number of spaces */ return count; } Procedures Questions: How does one... pass parameters? pass back the return value? start executing the function? return from the function? use registers? First Attempt pass parameters? use register $4 pass back the return value? use register $2 start executing the function? use j return from the function? use j use registers? use any Assembly callee caller NumSpaces: addu $17,$0,$4 ... ... j NumSpaces $done: addu $2,$0,$16 Return: ... j Return ... Second Attempt Might want to call function from multiple places... start executing the function? use jal return from the function? use jr Assembly callee caller NumSpaces: addu $17,$0,$4 ... ... jal NumSpaces $done: addu $2,$0,$16 ... jr $31 ... What About Recursion? C code int NumSpaces (char *s) { int count; if (!(*s)) return 0; count = NumSpaces (s+1); if (*s == ) count++; return count; } see also: Recursion What About Recursion? NumSpaces: addu $17,$0,$4 # s = argument1 lbu $8, 0($17) # temp = *s beq $8,$0,$done # if *s == 0 goto done addiu $4,$4,1 # argument = s+1 jal NumSpaces # call NumSpaces # count is $2 li $9,32 # temp = bne $9,$8,$skipinc # if *s != # goto skipinc addiu $2,$2,1 # count++ $skipinc: jr $31 # return $done: li $2,0 # return value = 0 jr $31 # return Stacks: Last-In First-Out...
View Full Document

This note was uploaded on 06/25/2008 for the course ECE 3140 taught by Professor Mckee/long during the Spring '07 term at Cornell University (Engineering School).

Page1 / 19

l04 - Procedures Let's try to make the code reusable... C...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online