Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment, you will reverse engineer several x86-64 assembly language functions. The goal is to increase your knowledge of assembly language and help you

In this assignment, you will "reverse engineer" several x86-64 assembly language functions. The goal is to increase your knowledge of assembly language and help you develop the ability to read assembly language code and understand what it does.

Your task: You will find a file called hw5.s , which contains x64 (assembly language) implementations of 9 functions. They are called f1, f2, f3, etc. I generated hw5.s by running gcc on my own C source code, using the S and O2 flags. Your task is to write C implementations of these 9 functions, so that your C code emulates the assembly language versions. Each C function that you write will be counted as correct if your function returns the same value as its assembly language version, provided they are both passed the same parameter(s). The exact way in which your C functions are implemented does not have to match the assembly language code. In other words, if you run gcc on your own C code with -S and -O2, the assembly language produced from your code does not have to be an exact match with mine.

PROTOTYPE FOR FUNCTIONS:

#include #include #include "hw5.h"

TEMPLATES:

int f1(int a, int b, int c) { return 0; // replace this }

void f2(int *a, int b) {

}

int f3(long a) { return 0; // replace this }

int f4(int a, int b) { return 0; // replace this }

int f5(char *a) { return 0; // replace this

}

char *f6(char *a, char *b) { return a; // replace this }

char *f7(char *a, char *b) { return a; // replace this }

void f8(int *a, int b) {

}

int f9(int *a, int b) { return 0; // replace this }

ASSEMBLEY LANGUAGE:

.file "hw5sol.c" .text .p2align 4,,15 .globl f1 .type f1, @function f1: .LFB25: .cfi_startproc cmpl %esi, %edi je .L3 cmpl %edx, %esi je .L3 xorl %eax, %eax cmpl %edx, %edi setne %al ret .p2align 4,,10 .p2align 3 .L3: xorl %eax, %eax ret .cfi_endproc .LFE25: .size f1, .-f1 .p2align 4,,15 .globl f2 .type f2, @function f2: .LFB26: .cfi_startproc addl %esi, (%rdi) ret .cfi_endproc .LFE26: .size f2, .-f2 .p2align 4,,15 .globl f3 .type f3, @function f3: .LFB27: .cfi_startproc xorl %eax, %eax testq %rdi, %rdi setg %al ret .cfi_endproc .LFE27: .size f3, .-f3 .p2align 4,,15 .globl f4 .type f4, @function f4: .LFB28: .cfi_startproc cmpl %esi, %edi movl %edi, %eax jl .L14 .p2align 4,,10 .p2align 3 .L15: subl %esi, %eax cmpl %eax, %esi jle .L15 .L14: rep ret .cfi_endproc .LFE28: .size f4, .-f4 .p2align 4,,15 .globl f5 .type f5, @function f5: .LFB29: .cfi_startproc cmpb $0, (%rdi) je .L19 addq $1, %rdi xorl %eax, %eax .p2align 4,,10 .p2align 3 .L18: addq $1, %rdi addl $1, %eax cmpb $0, -1(%rdi) jne .L18 rep ret .L19: xorl %eax, %eax ret .cfi_endproc .LFE29: .size f5, .-f5 .p2align 4,,15 .globl f6 .type f6, @function f6: .LFB30: .cfi_startproc movzbl (%rsi), %edx movq %rdi, %rax testb %dl, %dl je .L23 movl $1, %ecx xorl %r9d, %r9d jmp .L22 .p2align 4,,10 .p2align 3 .L25: movq %rcx, %r9 movq %r8, %rcx .L22: movb %dl, (%rax,%r9) movzbl (%rsi,%rcx), %edx leaq 1(%rcx), %r8 testb %dl, %dl jne .L25 .L21: movb $0, (%rax,%rcx) ret .L23: xorl %ecx, %ecx jmp .L21 .cfi_endproc .LFE30: .size f6, .-f6 .p2align 4,,15 .globl f7 .type f7, @function f7: .LFB31: .cfi_startproc cmpb $0, (%rdi) movq %rdi, %rax je .L32 leaq 1(%rdi), %rdx xorl %r8d, %r8d .p2align 4,,10 .p2align 3 .L28: movq %rdx, %r9 addq $1, %rdx addl $1, %r8d cmpb $0, -1(%rdx) jne .L28 .L27: movzbl (%rsi), %ecx testb %cl, %cl je .L29 movslq %r8d, %r9 xorl %edx, %edx addq %rax, %r9 .p2align 4,,10 .p2align 3 .L31: movb %cl, (%r9,%rdx) addq $1, %rdx addl $1, %r8d movzbl (%rsi,%rdx), %ecx testb %cl, %cl jne .L31 movslq %r8d, %r8 leaq (%rax,%r8), %r9 .L29: movb $0, (%r9) ret .L32: movq %rdi, %r9 xorl %r8d, %r8d jmp .L27 .cfi_endproc .LFE31: .size f7, .-f7 .p2align 4,,15 .globl f8 .type f8, @function f8: .LFB32: .cfi_startproc testl %esi, %esi leal 1(%rsi), %edx movl $1, %eax jle .L36 .p2align 4,,10 .p2align 3 .L40: movl %eax, (%rdi) addl $1, %eax addq $4, %rdi cmpl %edx, %eax jne .L40 .L36: rep ret .cfi_endproc .LFE32: .size f8, .-f8 .p2align 4,,15 .globl f9 .type f9, @function f9: .LFB33: .cfi_startproc cmpl $1, %esi jle .L45 movl (%rdi), %eax cmpl %eax, 4(%rdi) jl .L47 leal -2(%rsi), %eax addq $8, %rdi leaq (%rdi,%rax,4), %rdx jmp .L43 .p2align 4,,10 .p2align 3 .L44: movl (%rdi), %eax addq $4, %rdi cmpl -8(%rdi), %eax jl .L47 .L43: cmpq %rdx, %rdi jne .L44 .L45: movl $1, %eax ret .p2align 4,,10 .p2align 3 .L47: xorl %eax, %eax ret .cfi_endproc .LFE33: .size f9, .-f9 .ident "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-4)" .section .note.GNU-stack,"",@progbits

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database System

Authors: Elmasri Ramez And Navathe Shamkant

7th Edition

978-9332582705

More Books

Students also viewed these Databases questions