Question

You are tasked to calculate a specific algebraic expansion, i.e. compute the value of f and g for the expression:

( I have just to use loop add and sub no other functions)

f = AC – 3C+ BD - 4AB

g = A^{2}C+B^{2}D^{2}

without using any intrinsic multiplication instructions. More formally, write MIPS assembly code that accepts four positive integers a, b, c, and d as input parameters. The code shall execute in MARS to prompt the user to enter four positive integers represented in decimal, each separated by the Enter key. The program shall calculate f=(AC3-3C2+BD2-4B) and g=(A2C+B2D2) using your own self-written multiplication routine. The program will then output f in decimal and binary, using syscall routines for each output. Hint: MARS supports output in binary format using the appropriate parameters with syscall. For the Clanguage prototype, there are many approaches to print a value in binary in C. Here is my favorite singleline-in-a-loop solution that you can use with any C-compiler to print x in binary, shown with x=9 as an example:

main() { int i,x=9; for (i=31;i>=0;i--) { /*

put char with ASCII code for either 0 or 1 by adding 1 for the ith bit to ASCII of '0'

*/ put char('0' + ((x>>i) & 1)); } return(0); } Y

ou can use the above in your C-code prototype or write your own binary output routine in C if you want.

Sample output for Part A is:

Enter 4 integers for A,B,C,D respectively

15

9

12

3

f_ten = 25533

f_two = 00000000000000000110001110111101

g_ten = 3429

g_two = 00000000000000000000110101100101

0 0
Add a comment Improve this question Transcribed image text
Answer #1

.file "dtob.c"
.text
.globl f
.type f, @function
f:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
movl %edx, -12(%rbp)
movl %ecx, -16(%rbp)
movl -12(%rbp), %eax
imull -12(%rbp), %eax
imull -12(%rbp), %eax
imull -4(%rbp), %eax
movl %eax, %ecx
movl -12(%rbp), %eax
imull -12(%rbp), %eax
movl %eax, %edx
movl %edx, %eax
addl %eax, %eax
addl %edx, %eax
negl %eax
leal (%rcx,%rax), %edx
movl -16(%rbp), %eax
imull -16(%rbp), %eax
imull -8(%rbp), %eax
leal (%rdx,%rax), %ecx
movl -8(%rbp), %edx
movl $0, %eax
subl %edx, %eax
sall $2, %eax
addl %ecx, %eax
cltq
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size f, .-f
.globl g
.type g, @function
g:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
movl %edx, -12(%rbp)
movl %ecx, -16(%rbp)
movl -4(%rbp), %eax
imull -4(%rbp), %eax
imull -12(%rbp), %eax
movl %eax, %edx
movl -8(%rbp), %eax
imull -8(%rbp), %eax
movl %eax, %ecx
movl -16(%rbp), %eax
imull -16(%rbp), %eax
imull %ecx, %eax
addl %edx, %eax
cltq
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1:
.size g, .-g
.globl dec_to_bin
.type dec_to_bin, @function
dec_to_bin:
.LFB2:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $32, %rsp
movq %rdi, -24(%rbp)
movl $31, -4(%rbp)
jmp .L6
.L7:
movl -4(%rbp), %eax
movq -24(%rbp), %rdx
movl %eax, %ecx
sarq %cl, %rdx
movq %rdx, %rax
andl $1, %eax
addl $48, %eax
movl %eax, %edi
call putchar
subl $1, -4(%rbp)
.L6:
cmpl $0, -4(%rbp)
jns .L7
movl $10, %edi
call putchar
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE2:
.size dec_to_bin, .-dec_to_bin
.section .rodata
.LC0:
.string "Enter a,b,c and d vlaues : "
.LC1:
.string "%d%d%d%d"
.LC2:
.string "f_ten = %ld\n"
.LC3:
.string "f_two = "
.LC4:
.string "g_ten = %ld\n"
.LC5:
.string "g_two = "
.text
.globl main
.type main, @function
main:
.LFB3:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $32, %rsp
movl $.LC0, %edi
call puts
leaq -20(%rbp), %rsi
leaq -24(%rbp), %rcx
leaq -28(%rbp), %rdx
leaq -32(%rbp), %rax
movq %rsi, %r8
movq %rax, %rsi
movl $.LC1, %edi
movl $0, %eax
call __isoc99_scanf
movl -20(%rbp), %ecx
movl -24(%rbp), %edx
movl -28(%rbp), %esi
movl -32(%rbp), %eax
movl %eax, %edi
call f
movq %rax, -16(%rbp)
movl -20(%rbp), %ecx
movl -24(%rbp), %edx
movl -28(%rbp), %esi
movl -32(%rbp), %eax
movl %eax, %edi
call g
movq %rax, -8(%rbp)
movq -16(%rbp), %rax
movq %rax, %rsi
movl $.LC2, %edi
movl $0, %eax
call printf
movl $.LC3, %edi
movl $0, %eax
call printf
movq -16(%rbp), %rax
movq %rax, %rdi
call dec_to_bin
movq -8(%rbp), %rax
movq %rax, %rsi
movl $.LC4, %edi
movl $0, %eax
call printf
movl $.LC5, %edi
movl $0, %eax
call printf
movq -8(%rbp), %rax
movq %rax, %rdi
call dec_to_bin
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE3:
.size main, .-main
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4"
.section .note.GNU-stack,"",@progbits

Add a comment
Know the answer?
Add Answer to:
You are tasked to calculate a specific algebraic expansion, i.e. compute the value of f and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone provide detailed comments on what this code is doing for each line? I posted...

    Can someone provide detailed comments on what this code is doing for each line? I posted the assignment below. I am having trouble learning and getting assistance because I keep getting answers provided w/ good information on how the problem was solved. I have spent too much on tutoring this month and I am relying heavily on assistance here. Can you please assist with helping me understand each line of code that was written here? Also- is there an easier...

  • In the following code I give, everytime it will output ")". Input : ((A+B)*((C-D)/(E^F))) Expected output...

    In the following code I give, everytime it will output ")". Input : ((A+B)*((C-D)/(E^F))) Expected output : ((A+B)*((C-D)/(E^F))) A+B*C-D/E^F my output : ) My code : class Et: def __init__(self , value): self.value = value self.left = None self.right = None def isOperator(c): if (c == '+' or c == '-' or c == '*' or c == '/' or c == '^' ): return True else: return False def inorder(t): if t is not None: inorder(t.left) print (t.value) inorder(t.right)...

  • In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first'...

    In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and 'last' is the last index void writeArrayBackward(const char anArray[], int first, int last) { int i = 0; for (i = last; i >= first; i--) { std::cout << anArray[i]; } std::cout << std::endl; } // test driver int main() { const char *s = "abc123"; writeArrayBackward(s, 0, strlen(s) - 1); } // Using the...

  • C programming lab: Description: In this lab you will write a program that will contain two...

    C programming lab: Description: In this lab you will write a program that will contain two functions, setlsbs() and getlsbs(). These functions will use bitwise operators to embed and extract "hidden" bits in a character array. Write a program to test your functions as follows: Obtain a random number seed from the command line of your program using command line arguments. Initialize an array p of 8 unsigned char with random numbers from 0 to 255 Initialize a separate unsigned...

  • You shall develop a grammar and implement a parser which recognizes valid statements as described below in the assignment specification. You may develop your code using C, C++. The test file include...

    You shall develop a grammar and implement a parser which recognizes valid statements as described below in the assignment specification. You may develop your code using C, C++. The test file include these expressions below. The first six should pass and the rest should fail: first = one1 + two2 - three3 / four4 ; second = one1 * (two2 * three3) ; second = one1 * (two2 * three3) ; third = ONE + twenty - three3 ; third...

  • Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C -...

    Question 1 An array is NOT: A - Made up of different data types. B - Subscripted by integers. C - A consecutive group of memory chunks. D - None of the choices. Question 2 How many times is the body of the loop executed? int i=1; while(true) { cout << i; if(++i==5) break; } A - Forever B - 4 C - 5 D - 6 E - 0 Question 3 What is wrong with the following piece of...

  • Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three...

    Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities: Covert a binary string to corresponding positive integers Convert a positive integer to its binary representation Add two binary numbers, both numbers are represented as a string of 0s and 1s To reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the...

  • C PROGRAM When you print out the old and new bitsets, which are of type unsigned...

    C PROGRAM When you print out the old and new bitsets, which are of type unsigned char, please use the %p control character in the printff statement rather than %x or %d or %c. The compiler will complain, but we don't always listen to them anyway. The difference is it will print the bitset out in hex with a preceeding %0x. unsigned char bitset = 0x14 ; printf("Bitsrt is %p\n", bitset) ; results in Bitset is 0x14, which is what...

  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

  • Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you...

    Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you must copy and paste to cloud9. Do not change the provided complete functions, or the function stub headers / return values. Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT