Please write MIPS program that runs in QtSpim (ex: MipsFile.s)
Write a MIPS program that will read in a base (as an integer) and a value (nonnegative integer but as an ASCII string) in that base and print out the decimal value; you must implement a function (which accepts a base and an address for a string as parameters, and returns the value) and call the function from the main program. The base will be given in decimal and will be between 2 and 36, and the digits for the values will be in set [0…9, a…z]. You can assume that no overflow will occur and the value is valid in the given base.
An example run will be:
Enter a base (between 2 and 36 in decimal): 2
Enter a number in base 2: 101
The value in decimal is: 5
The program needs to be able to test 3 different cases and run in QtSpim like a .s file
here is a c program that shows the functionality
#include <ctype.h> #include <stdio.h> #include <string.h> int convert2dec(char *str, int base) { int j, val; val = 0; j = 0; while (str[j] > 13) { if (str[j] > 57) val = val * base + str[j]-87; else val = val * base + str[j] - 48; j++; } return val; } int main(int argc, char *argv[]) { int X; char str[256]; printf("Please the base (in decimal): "); scanf("%d", &X); printf("Please enter a nonnegative number: "); scanf("%s", str); printf("The decimal value is %d.\n", convert2dec(str,X)); return 0; }
Answer:
MIPS Assembly Language Program :
convert2dec(char*, int):
push
rbp
mov rbp, rsp
mov QWORD PTR [rbp-24], rdi
mov DWORD PTR [rbp-28], esi
mov DWORD PTR [rbp-8], 0
mov DWORD PTR [rbp-4], 0
.L5:
mov eax, DWORD PTR [rbp-4]
movsx rdx,
eax
mov rax, QWORD PTR [rbp-24]
add rax, rdx
movzx eax,
BYTE PTR [rax]
cmp al, 13
jle .L2
mov eax, DWORD PTR [rbp-4]
movsx rdx,
eax
mov rax, QWORD PTR [rbp-24]
add rax, rdx
movzx eax,
BYTE PTR [rax]
cmp al, 57
jle .L3
mov eax, DWORD PTR [rbp-8]
imul
eax, DWORD PTR [rbp-28]
mov edx, eax
mov eax, DWORD PTR [rbp-4]
movsx rcx,
eax
mov rax, QWORD PTR [rbp-24]
add rax, rcx
movzx eax,
BYTE PTR [rax]
movsx eax,
al
add eax, edx
sub eax, 87
mov DWORD PTR [rbp-8], eax
jmp .L4
.L3:
mov eax, DWORD PTR [rbp-8]
imul
eax, DWORD PTR [rbp-28]
mov edx, eax
mov eax, DWORD PTR [rbp-4]
movsx rcx,
eax
mov rax, QWORD PTR [rbp-24]
add rax, rcx
movzx eax,
BYTE PTR [rax]
movsx eax,
al
add eax, edx
sub eax, 48
mov DWORD PTR [rbp-8], eax
.L4:
add DWORD PTR [rbp-4], 1
jmp .L5
.L2:
mov eax, DWORD PTR [rbp-8]
pop rbp
ret
.LC0:
.string "Please the base
(in decimal): "
.LC1:
.string "%d"
.LC2:
.string "Please enter a
nonnegative number: "
.LC3:
.string "%s"
.LC4:
.string "The decimal
value is %d.\n"
main:
push
rbp
mov rbp, rsp
sub rsp, 288
mov DWORD PTR [rbp-276], edi
mov QWORD PTR [rbp-288], rsi
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call
printf
lea rax, [rbp-4]
mov rsi, rax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call
scanf
mov edi, OFFSET FLAT:.LC2
mov eax, 0
call
printf
lea rax, [rbp-272]
mov rsi, rax
mov edi, OFFSET FLAT:.LC3
mov eax, 0
call
scanf
mov edx, DWORD PTR [rbp-4]
lea rax, [rbp-272]
mov esi, edx
mov rdi, rax
call
convert2dec(char*, int)
mov esi, eax
mov edi, OFFSET FLAT:.LC4
mov eax, 0
call
printf
mov eax, 0
leave
ret
Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS program that will...
Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){ char str[1000], ch; int i, frequency = 0; clock_t t; struct timespec ts, ts2; printf("Enter a string: "); gets(str); int len = strlen(str); printf("Enter a character...
The following code is a C Program that is written for encrypting and decrypting a string. provide a full explanation of the working flow of the program. #include <stdio.h> int main() { int i, x; char str[100]; printf("\n Please enter a valid string: \t"); gets (str); printf ("\n Please choose one of the following options: \n"); printf ("1 = Encrypt the given string. \n"); printf("2 = Decrypt the entered string. \n"); scanf("%d",&x); // using switch case statements switch (x) {...
can u please solve it in c++ format,,,, general format which is #include<stdio.h> .......printf and scanf format dont worry about the answers i have down Q3, What is the output of the program shown below and explain why. #include #include <stdio.h> <string.h> int main(void) ( char str[ 39764 int N strlen(str) int nunj for (int i-0; i t Nǐ.de+ printf(") num (str[] 'e); I/ digits range in the ASCII code is 48-57 if (nue > e) f 9 printf( Xdin,...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> #define MAX_LEN 255 int numWords(char *str); int numDigit(char *str); int numUppltr(char *str); int numLwrltr(char *str); int punChar(char *str); char*readString(char *str); int main() { char givString[MAX_LEN]; puts("Enter your string(Max 255 characters):"); //readString(givString); gets(givString); printf("\nnumber of words :%d",numWords(givString)); printf("\nnumber of uppercase letters %d",numUppltr(givString)); printf("\nnumber of lowercase letters %d",numLwrltr(givString)); printf("\nnumber of punctuations %d\n",punChar(givString)); printf("\nnumber of digits:%d\n",numDigit(givString)); system("pause"); return 0; } char *readString(char *str) { int ch, i=0; while((ch=getchar())!=EOF && ch!='\n') { if(i) { str[i]=ch; i++; }...
Write a function that can return the length of a string in C language. Please leave comments to explain the code and verify that it runs properly... Example: Input String: China The length of the string is 5. #include <stdio.h> int length(char * p) { /write your code here } main(){ int len; char str[20]; printf("Input string:"); scanf("%s", str); len = length(str); printf("The length of string is %d. ", len); getchar(); ...
Please help modify my C program to be able to answer these questions, it seems the spacing and some functions arn't working as planeed. Please do NOT copy and paste other work as the answer, I need my source code to be modified. Source code: #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { char title[50]; char col1[50]; char col2[50]; int point[50]; char names[50][50]; printf("Enter a title for the data:\n"); fgets (title, 50, stdin); printf("You entered: %s\n", title);...
Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...
OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...
Looking for help understanding how this example program flows and works. Can you write comments next to each line of code explaining what it does? Why does it use a switch? why not set the roman numerals as constants instead of a switch? Thank you!! //CPP program for converting roman into integers #include <iostream> #include <fstream> #include<cctype> #include<cstdlib> #include<string.h> using namespace std; int value(char roman) { switch(roman) { case 'I':return 1; case 'V':return 5; case 'X':return 10; case 'L':return 50;...
In C Set hasDigit to true if the 3-character passCode contains a digit. 1 #include «stdio.h> 2 #include«string.h> 3 #include «stdbool.h 4 #include<ctype.h> 6 int main(void) 7 bool hasDigit; 8 char passCode501; 10 11 12 hasDigit false; scanf("%s", &passcode); 13 Your solution goes here * 14 15 if (hasDigit) 16 printf("Has a digit.n 4 #include <ctype.h> 6 int main (void) 1 8 char passCode [50]; bool hasDigit; hasDigit- false; 10 11 scanf("%s",&passcode); 12 13 14 15 if (hasDigit) 16 17...