You are tasked to calculate a specific algebraic expansion, i.e. compute the val
ID: 3891007 • Letter: Y
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)
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
f = AC – 3C+ BD - 4ABExplanation / Answer
.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 "
.LC3:
.string "f_two = "
.LC4:
.string "g_ten = %ld "
.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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.