Subjects
PRICING
COURSES
SIGN IN
Start Free Trial
Brian G.
Computer Science Graduate Student
Tutor Satisfaction Guarantee
MIPS
TutorMe
Question:

Fibonacci. Go->

Brian G.
Answer:

####################### Text Section ####################### .text .globl main main: addi $a0, $zero, 1 # jal do_fib # fib(1) add $s0, $v0, $zero # $s0 = fib(1) = 1 addi $a0, $zero, 4 # jal do_fib # fib(4) add $s1, $v0, $zero # $s1 = fib(4) = 5 addi $a0, $zero, 8 # jal do_fib # fib(8) add $s2, $v0, $zero # $s2 = fib(8) = 34 exit: li $v0 10 syscall #----------- fib(n) = 1 or fib(n-1) + fib(n-2) ------------# #----------- expects n in $a0 ------------# #----------- returns ans in $v0 ------------# # prologue do_fib: addi $sp, $sp, -20 # move stack pointer sw $s0, 0($sp) sw $s1, 4($sp) sw $s2, 8($sp) sw $s3, 12($sp) sw $ra, 16($sp) # body of the function add $s0, $a0, $zero # $s0 = n slti $t0, $s0, 2 # check base bne $t0, $zero, base # if n < 2 do base # recursive addi $a0, $s0, -1 # $a0 = n - 1 jal do_fib # fib(n-1) add $s2, $v0, $zero # $s2 = fib(n-1) addi $a0, $s0, -2 # $a0 = n - 2 jal do_fib # fib(n-2) add $s3, $v0, $zero # $s3 = fib(n-2) add $v0, $s2, $s3 # $v0 = fib(n-1) + fib(n-2) j return # all done base: addi $v0, $zero, 1 # base return: #epilogue lw $s0, 0($sp) # pop stack lw $s1, 4($sp) lw $s2, 8($sp) lw $s3, 12($sp) lw $ra, 16($sp) addi $sp, $sp, 20 # move stack pointer jr $ra # return #----------- end fib(n) ------------------# ####################### Data Section (unused) ####################### # .data

C++ Programming
TutorMe
Question:

How does pass by reference work

Brian G.
Answer:

an '&' is placed before the parameter name in the function header. This passes the address of the variable to the function, so that modifications in the function are kept when it returns. In the function the variable is treated normally (i.e. not as a pointer).

C Programming
TutorMe
Question:

Write a program to convert a decimal number to binary

Brian G.
Answer:

#include <stdio.h> void get_input(int *decimal); //int get_input(); void convert_to_binary(int decimal, char *binary, int *bit_sig_p); //int convert_to_binary(int decimal, char *binary, int bit_sig); //input will be one decimal number to convert to binary int main(void) { int decimal, i; int bit_sig = 15; char binary[16]; for(i = 0; i < 15; i++) binary[i] = '0'; binary[bit_sig--] = '\0'; printf("\n"); get_input(&decimal); //decimal = get_input(); //bit_sig = convert_to_binary(decimal, binary, bit_sig); convert_to_binary(decimal, binary, &bit_sig); printf("The binary form of %d is %s\n\n", decimal, binary); return 0; } //pass by reference void get_input(int *decimal) { printf("Please enter an integer: "); scanf("%d", decimal); } /*// return int get_input() { printf("Please enter an integer: "); scanf("%d", &decimal); return decimal; } */ // pass by reference void convert_to_binary(int decimal, char *binary, int *bit_sig_p){ int dividend; int remain; dividend = decimal / 2; remain = decimal % 2; if (remain == 0) binary[ (*bit_sig_p)-- ] = '0'; else binary[ (*bit_sig_p)-- ] = '1'; if (dividend != 0) convert_to_binary(dividend, binary, bit_sig_p); } /*// pass by value and return int convert_to_binary(int decimal, char *binary, int bit_sig){ int dividend; int remain; dividend = decimal / 2; remain = decimal % 2; if (remain == 0) binary[ bit_sig-- ] = '0'; else binary[ bit_sig-- ] = '1'; if (dividend != 0) bit_sig = convert_to_binary(dividend, binary, bit_sig); return bit_sig; } */

Send a message explaining your
needs and Brian will reply soon.
Contact Brian
Ready now? Request a lesson.
Start Session
FAQs
What is a lesson?
A lesson is virtual lesson space on our platform where you and a tutor can communicate. You'll have the option to communicate using video/audio as well as text chat. You can also upload documents, edit papers in real time and use our cutting-edge virtual whiteboard.
How do I begin a lesson?
If the tutor is currently online, you can click the "Start Session" button above. If they are offline, you can always send them a message to schedule a lesson.
Who are TutorMe tutors?
Many of our tutors are current college students or recent graduates of top-tier universities like MIT, Harvard and USC. TutorMe has thousands of top-quality tutors available to work with you.