Question

Write the C Code for the Intel assembley below. Note: Don't copy and paste anything from...

Write the C Code for the Intel assembley below. Note: Don't copy and paste anything from github because it won't be the right answer.

        .file   "mystery.c"
        .comm   num,1600,32
        .text
        .globl  add
        .type   add, @function
add:
.LFB2:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        movq    %rdi, -8(%rbp)
        movq    %rsi, -16(%rbp)
        movq    -16(%rbp), %rax
        movq    -8(%rbp), %rdx
        addq    %rdx, %rax
        popq    %rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
.LFE2:
        .size   add, .-add
        .globl  dothething
        .type   dothething, @function
dothething:
.LFB3:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        pushq   %rbx
        subq    $40, %rsp
        .cfi_offset 3, -24
        movq    %rdi, -40(%rbp)
        movq    -40(%rbp), %rax
        movq    num(,%rax,8), %rax
        cmpq    $-1, %rax
        je      .L4
        movq    -40(%rbp), %rax
        movq    num(,%rax,8), %rax
        jmp     .L5
.L4:
        movq    $-1, -24(%rbp)
        cmpq    $0, -40(%rbp)
        jne     .L6
        movq    $0, -24(%rbp)
        jmp     .L7
.L6:
        cmpq    $1, -40(%rbp)
        jne     .L8
        movq    $1, -24(%rbp)
        jmp     .L7
.L8:
        movq    -40(%rbp), %rax
        subq    $2, %rax
        movq    %rax, %rdi
        call    dothething
        movq    %rax, %rbx
        movq    -40(%rbp), %rax
        subq    $1, %rax
        movq    %rax, %rdi
        call    dothething
        movq    %rbx, %rsi
        movq    %rax, %rdi
        call    add
        movq    %rax, -24(%rbp)
.L7:
        movq    -40(%rbp), %rax
        movq    num(,%rax,8), %rax
        cmpq    $-1, %rax
        jne     .L9
        movq    -40(%rbp), %rax
        movq    -24(%rbp), %rdx
        movq    %rdx, num(,%rax,8)
.L9:
        movq    -40(%rbp), %rax
        movq    num(,%rax,8), %rax
.L5:
        addq    $40, %rsp
        popq    %rbx
        popq    %rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
.LFE3:
        .size   dothething, .-dothething
        .section        .rodata
.LC0:
        .string "Value:   %d\n"
        .text
        .globl  main
        .type   main, @function
main:
.LFB4:
        .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    %edi, -20(%rbp)
        movq    %rsi, -32(%rbp)
        movq    -32(%rbp), %rax
        addq    $8, %rax
        movq    (%rax), %rax
        movq    %rax, %rdi
        call    atoi
        movl    %eax, -8(%rbp)
        movl    $0, -4(%rbp)
        jmp     .L11
.L12:
        movl    -4(%rbp), %eax
        cltq
        movq    $-1, num(,%rax,8)
        addl    $1, -4(%rbp)
.L11:
        cmpl    $199, -4(%rbp)
        jle     .L12
        movl    -8(%rbp), %eax
        cltq
        movq    %rax, %rdi
        call    dothething
        movq    %rax, %rsi
        movl    $.LC0, %edi
        movl    $0, %eax
        call    printf
        movl    $0, %eax
        leave
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
.LFE4:
        .size   main, .-main
        .ident  "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-4)"
        .section        .note.GNU-stack,"",@progbits
0 0
Add a comment Improve this question Transcribed image text
Answer #1

int arg1, arg2, add ;

__asm__ ( "addl %%ebx, %%eax;"

: "=a" (add)

: "a" (arg1), "b" (arg2) );

#include <stdio.h>

int main() {

int arg1, arg2, add, sub, mul, quo, rem ;

printf( "Enter two integer numbers : " );

scanf( "%d%d", &arg1, &arg2 );

/* Perform Addition, Subtraction, Multiplication & Division */

__asm__ ( "addl %%ebx, %%eax;" : "=a" (add) : "a" (arg1) , "b" (arg2) );

__asm__ ( "subl %%ebx, %%eax;" : "=a" (sub) : "a" (arg1) , "b" (arg2) );

__asm__ ( "imull %%ebx, %%eax;" : "=a" (mul) : "a" (arg1) , "b" (arg2) );

__asm__ ( "movl $0x0, %%edx;"

"movl %2, %%eax;"

"movl %3, %%ebx;"

"idivl %%ebx;" : "=a" (quo), "=d" (rem) : "g" (arg1), "g" (arg2) );

printf( "%d + %d = %d\n", arg1, arg2, add );

printf( "%d - %d = %d\n", arg1, arg2, sub );

printf( "%d * %d = %d\n", arg1, arg2, mul );

printf( "%d / %d = %d\n", arg1, arg2, quo );

printf( "%d %% %d = %d\n", arg1, arg2, rem );

return 0 ;

}

#include <stdio.h>

int gcd( int a, int b ) {

int result ;

/* Compute Greatest Common Divisor using Euclid's Algorithm */

__asm__ __volatile__ ( "movl %1, %%eax;"

"movl %2, %%ebx;"

"CONTD: cmpl $0, %%ebx;"

"je DONE;"

"xorl %%edx, %%edx;"

"idivl %%ebx;"

"movl %%ebx, %%eax;"

"movl %%edx, %%ebx;"

"jmp CONTD;"

"DONE: movl %%eax, %0;" : "=g" (result) : "g" (a), "g" (b)

);

return result ;

}

int main() {

int first, second ;

printf( "Enter two integers : " ) ;

scanf( "%d%d", &first, &second );

printf( "GCD of %d & %d is %d\n", first, second, gcd(first, second) ) ;

return 0 ;

}

#include <stdio.h>

int main() {

float arg1, arg2, add, sub, mul, div ;

printf( "Enter two numbers : " );

scanf( "%f%f", &arg1, &arg2 );

/* Perform floating point Addition, Subtraction, Multiplication & Division */

__asm__ ( "fld %1;"

"fld %2;"

"fadd;"

"fstp %0;" : "=g" (add) : "g" (arg1), "g" (arg2) ) ;

__asm__ ( "fld %2;"

"fld %1;"

"fsub;"

"fstp %0;" : "=g" (sub) : "g" (arg1), "g" (arg2) ) ;

__asm__ ( "fld %1;"

"fld %2;"

"fmul;"

"fstp %0;" : "=g" (mul) : "g" (arg1), "g" (arg2) ) ;

__asm__ ( "fld %2;"

"fld %1;"

"fdiv;"

"fstp %0;" : "=g" (div) : "g" (arg1), "g" (arg2) ) ;

printf( "%f + %f = %f\n", arg1, arg2, add );

printf( "%f - %f = %f\n", arg1, arg2, sub );

printf( "%f * %f = %f\n", arg1, arg2, mul );

printf( "%f / %f = %f\n", arg1, arg2, div );

return 0 ;

}

#include <stdio.h>

float sinx( float degree ) {

float result, two_right_angles = 180.0f ;

/* Convert angle from degrees to radians and then calculate sin value */

__asm__ __volatile__ ( "fld %1;"

"fld %2;"

"fldpi;"

"fmul;"

"fdiv;"

"fsin;"

"fstp %0;" : "=g" (result) :

"g"(two_right_angles), "g" (degree)

) ;

return result ;

}

float cosx( float degree ) {

float result, two_right_angles = 180.0f, radians ;

/* Convert angle from degrees to radians and then calculate cos value */

__asm__ __volatile__ ( "fld %1;"

"fld %2;"

"fldpi;"

"fmul;"

"fdiv;"

"fstp %0;" : "=g" (radians) :

"g"(two_right_angles), "g" (degree)

) ;

__asm__ __volatile__ ( "fld %1;"

"fcos;"

"fstp %0;" : "=g" (result) : "g" (radians)

) ;

return result ;

}

float square_root( float val ) {

float result ;

__asm__ __volatile__ ( "fld %1;"

"fsqrt;"

"fstp %0;" : "=g" (result) : "g" (val)

) ;

return result ;

}

int main() {

float theta ;

printf( "Enter theta in degrees : " ) ;

scanf( "%f", &theta ) ;

printf( "sinx(%f) = %f\n", theta, sinx( theta ) );

printf( "cosx(%f) = %f\n", theta, cosx( theta ) );

printf( "square_root(%f) = %f\n", theta, square_root( theta ) ) ;

return 0 ;

}

Add a comment
Know the answer?
Add Answer to:
Write the C Code for the Intel assembley below. Note: Don't copy and paste anything from...
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
  • You know the following assembly code snippet is from a recursive function in C. You also...

    You know the following assembly code snippet is from a recursive function in C. You also know that the stack contents at a particular point in time when we are in the recursive function are shown on the next page. Answer the following questions: a) how many Foo stack frames are on the stack? b) what is the return address back to the function that called Foo for the first time? c) what is the return address back into the...

  • Consider the following source code, where b, c, and d are constants declared with #define. You...

    Consider the following source code, where b, c, and d are constants declared with #define. You will need to determine the values for b, c, and d. struct lnode { char *str; struct lnode *next; } struct lnode A[b][c][d]; int store_ele(int h, int i, int j, struct lnode dest) { A[h][i][j] = dest; return sizeof(A);} On compiling this program (with -O2), GCC generates the following assembly code for the store_ele function: store_ele: movslq %edi, %rdi movslq %esi, %rsi movslq %edx,...

  • May you explain how you got the answer The following code sequence occurs near the beginning...

    May you explain how you got the answer The following code sequence occurs near the beginning of the assembly code generated by gcc for a c procedure: 9. 1, subq 2" movq 3" movq 4" movq 5. movq 6. movq 7. movq 8. movq 9. movq 10. movq $24,%rsp %rbx, (%rsp) %r12, 8(%rsp) %r13, 16(%rsp) 16(%rbp), %rbx 24(%rbp), %r13 (%rbx), %r12 (%r13), %rax 16(%rbp ) ,%rdx (%rdx), %rcx we see that just three registers (%rbx, %r12 and %r13) are saved...

  • Problem 2 Consider the following source code, where R, S, and T are constants declared with...

    Problem 2 Consider the following source code, where R, S, and T are constants declared with #define. long int AIRISJIT int store_ele(int h, int i, int j, long int 'dest) Alh][i]01-.dest; return sizeof(A) In compiling this program, GCC generates the following assembly code (with-02) store_ ele: movslq %esi, %rsi movslq %edi, %rdi movq (%rcx), %rax leaq (%rdi,%rdi,4), %rdi leaq (%rsi,%rsi,4), %rcx movslq %edx, %rdx addq %rcx, %rdx movq %rax, A( %rdx,8) movl $1120, %eax ret Create a text file to...

  • Binary Bomb phase 4 Dump of assembler code for function phase_4: > 0x0000000000400fe7 <+0>:     sub    $0x18,%rsp...

    Binary Bomb phase 4 Dump of assembler code for function phase_4: > 0x0000000000400fe7 <+0>:     sub    $0x18,%rsp    0x0000000000400feb <+4>:     lea    0x8(%rsp),%rcx    0x0000000000400ff0 <+9>:     lea    0xc(%rsp),%rdx    0x0000000000400ff5 <+14>:    mov    $0x40290d,%esi    0x0000000000400ffa <+19>:    mov    $0x0,%eax    0x0000000000400fff <+24>:    callq 0x400c00 <__isoc99_sscanf@plt>    0x0000000000401004 <+29>:    cmp    $0x2,%eax    0x0000000000401007 <+32>:    jne    0x401010 <phase_4+41>    0x0000000000401009 <+34>:    cmpl   $0xe,0xc(%rsp)    0x000000000040100e <+39>:    jbe    0x401015 <phase_4+46>    0x0000000000401010 <+41>:    callq 0x401662 <explode_bomb>    0x0000000000401015 <+46>:    mov    $0xe,%edx    0x000000000040101a <+51>:    mov    $0x0,%esi...

  • a) Write the following C function in Assembly. You must follow the System V 64-bit calling convention and use AT&T S...

    a) Write the following C function in Assembly. You must follow the System V 64-bit calling convention and use AT&T Syntax notation. Note: You cannot change the algorithm in any way so your assembly function must still be recursive. (20 points) long Catalan(long n) { long sum = 0; if (n == 0) return 1; for (int i = 0; i < n; i++) { sum += Catalan(i) * Catalan(n - i - 1); } return sum; } b) The...

  • Assembly language 64 bit please ! An example file for set up ==========+ ;| Data Segment...

    Assembly language 64 bit please ! An example file for set up ==========+ ;| Data Segment BEGINS Here | ;+======================================================================+ segment .data ;Code this expression: sum = num1+num2 num1 dq 0 ;left operand of the addition operation num2 dq 0 ;right operand of the addition operation sum dq 0 ;will hold the computed Sum value RetVal dq 0 ;Integer value RETURNED by function calls ;can be ignored or used as determined by the programmer ;Message string prompting for the keyboard...

  • #define SIZE 10 prob3: void prob3 (int mat [SIZE] [SIZE]) int r, c pushl movl tebp...

    #define SIZE 10 prob3: void prob3 (int mat [SIZE] [SIZE]) int r, c pushl movl tebp esp, ebp edi 2 pushl esi pushl ebx pushl $4, esp 8 (ebp), eax $1, (eax) subl movl mat [0] [0] 1; movl leal 40 (teax), Sebx movl $1, 40 (teax) 11 addl $80, teax 12 eax, -16 (tebp) movl 13 movl $1, tedi .L2 14 jmp 15 for (r 1; r<SIZE; r++) ( L5: 16 -16 (ebp), tebx movl 17 mat [r] [0]...

  • Below is the disassembled code. PLease help me to defuse the binary bomb phase_4 so the...

    Below is the disassembled code. PLease help me to defuse the binary bomb phase_4 so the right input should be  6 numbers with a certain pattern 08048cdb <phase_4>: 8048cdb: 53 push %ebx 8048cdc: 83 ec 38 sub $0x38,%esp 8048cdf: 8d 44 24 18 lea 0x18(%esp),%eax 8048ce3: 89 44 24 04 mov %eax,0x4(%esp) 8048ce7: 8b 44 24 40 mov 0x40(%esp),%eax 8048ceb: 89 04 24 mov %eax,(%esp) 8048cee: e8 11 07 00 00 call 8049404 <read_six_numbers> 8048cf3: 83 7c 24 18 00 cmpl...

  • Below is the disassembled code. PLease help me to defuse the binary bomb phase_7 08048e88 <pha...

    Below is the disassembled code. PLease help me to defuse the binary bomb phase_7 08048e88 <phase_7>: 8048e88: 83 ec 2c sub $0x2c,%esp 8048e8b: 8d 44 24 18 lea 0x18(%esp),%eax 8048e8f: 89 44 24 0c mov %eax,0xc(%esp) 8048e93: 8d 44 24 1c lea 0x1c(%esp),%eax 8048e97: 89 44 24 08 mov %eax,0x8(%esp) 8048e9b: c7 44 24 04 61 a6 04 movl $0x804a661,0x4(%esp) 8048ea2: 08 8048ea3: 8b 44 24 30 mov 0x30(%esp),%eax 8048ea7: 89 04 24 mov %eax,(%esp) 8048eaa: e8 c1 f9 ff...

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
ADVERTISEMENT