Skip to content

Commit

Permalink
ps1
Browse files Browse the repository at this point in the history
  • Loading branch information
L10yx authored May 10, 2024
1 parent 083b7e8 commit 6c083f5
Show file tree
Hide file tree
Showing 21 changed files with 812 additions and 0 deletions.
78 changes: 78 additions & 0 deletions ps1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Problem Set 1 - ECE391 spring 2024

### Logistics
Problem Set 1 is due **Tuesday 1/30 at 05:59:59 PM** in the master branch. Only one
person per group should have a `partners.txt` with **all** the netids of your partners on separate lines.
For this PS, form a group of **at least four students**. There is no cap on the maximum number of collaborators in the same group.
An example `partners.txt` would look like this if `elihf2` was submitting the group's solution.

elihf2 <br>
linghao3 <br>
yuey10 <br>
zhongbo2 <br>
yuezel2 <br>
dcaglar2 <br>


Note that if your netid is part of more than one `partners.txt`, you will recieve a 0 for the problem set.

**You can attempt this problem set on the Class VM (devel) or on any EWS Linux computer.**

### Problem 1: GNU Debugger (GDB) (5 pt)
Please write the command(s) you should use to achieve the following tasks in GDB.
1. Show the value of variable "test" in hex format.
2. Show the top four bytes on your stack word-by-word, e.g. it should look something like this "0x0102 0x0304", NOT "0x01020304".
3. Check all register values.
4. Set a breakpoint at "ece.c" line 391.
5. Connect to the test\_(no)debug vm in the lab setup.

Please write your solution in p1\_soln.txt with answers to each question on a separate line. For example, your p1\_soln.txt should be of the form
> answer 1
>
> answer 2
>
> ...
>
> answer 5
### Problem 2: C to Assembly Translation (10 pt)
Write x86 assembly code for the body of the `binarysearch` function found in `binarysearch.c`. Make sure to set up and tear down the stack frame as well as save and restore any callee/caller-saved registers yourself (if you use them). Assume caller-saved registers are saved prior to the `binarysearch_asm` function being called for the first time. Include comments (but don't overdo it!) in your assembly code to show the correspondence between the C code and your x86 code.

Also note that:
1. The `binarysearch_asm` function in `binarysearch_asm.S` is partially filled out for you, your job is to complete the function.
2. Please make sure your code and comments are easy to read. We reserve the right to take points off if your answer is too hard to follow.
3. You must synthesize your answer without the help of a computer. For example, you may not write the C code and then use the compiler to disassemble it. If you are caught doing this, you will receive a 0.
4. You must **translate** your code, a functionally equivalent algorithm with a different structure will recieve a 0.
5. You must write your solution in `p2/binarysearch_asm.S` and submit it through gitlab.

To build the code (no debug flag):
`$ make clean && make`

To run the code:
`$ ./binarysearch ./input_small.txt`

To build the code (debug flag):
`$ make clean && make debug`

To run the code (debug):
`$ gdb --args ./binarysearch ./input_small.txt`

### Problem 3: Assembly to C Translation (10 pt)
Write a C function equivalent to the x86 assembly function, `mystery_asm` found in `mystery_asm.S`.

1. Please make sure your code and comments are easy to read. We reserve the right to take points off if your answer is too hard to follow.
2. You must **translate** your code, a functionally equivalent algorithm with a different structure will recieve a 0.
3. You must write your solution in `p3/mystery.c` and submit it through gitlab.

To build the code (no debug flag):
`$ make clean && make`

To run the code:
`$ ./mystery ./input_1.txt`

To build the code (debug flag):
`$ make clean && make debug`

To run the code (debug):
`$ gdb --args ./mystery ./input_1.txt`

1 change: 1 addition & 0 deletions ps1/p1/grade.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5/5
5 changes: 5 additions & 0 deletions ps1/p1/p1_soln.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
print/x test
x/2xh $esp
info registers
break ece.c:391
target remote 10.0.2.2:1234
31 changes: 31 additions & 0 deletions ps1/p2/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Makefile C to ASM
# Andrew Smith
# 1/24/20


PROG := binarysearch

CFLAGS += -m32 -Wall -std=c99

.PHONY: clean

all: CFLAGS += -O0
all: $(PROG)

debug: CFLAGS += -O0 -g
debug: $(PROG)

$(PROG): binarysearch_asm.o binarysearch.o main.o
$(CC) -m32 $^ -o $@

binarysearch_asm.o: binarysearch_asm.S
$(CC) $(CFLAGS) -c $< -o $@

binarysearch.o: binarysearch.c
$(CC) $(CFLAGS) -c $< -o $@

main.o: main.c
$(CC) $(CFLAGS) -c $< -o $@

clean:
rm -f *.o $(PROG)
55 changes: 55 additions & 0 deletions ps1/p2/binarysearch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* tab:2
*
* search.c - Implementation of C Recursive Depth First Search
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* IN NO EVENT SHALL THE AUTHOR OR THE UNIVERSITY OF ILLINOIS BE LIABLE TO
* ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
* DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
* EVEN IF THE AUTHOR AND/OR THE UNIVERSITY OF ILLINOIS HAS BEEN ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE AUTHOR AND THE UNIVERSITY OF ILLINOIS SPECIFICALLY DISCLAIM ANY
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
* PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND NEITHER THE AUTHOR NOR
* THE UNIVERSITY OF ILLINOIS HAS ANY OBLIGATION TO PROVIDE MAINTENANCE,
* SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
* Author: Aamir Hasan
* Version: 1
* Creation Date: Fri Aug 30 2020
* Filename: search.c
* History:
* AS 1 Fri Aug 30 2020
* First written.
*/

#include "binarysearch.h"


int32_t binarySearch(int32_t* arr, int32_t md, int32_t low, int32_t high)
{
int mid = 0;
if (low <= high)
{
mid = (low + high) / 2;
if (md == arr[mid])
{
return 1;
}
else if (md < arr[mid])
{
return binarySearch(arr, md, low, mid - 1);
}
else
return binarySearch(arr, md, mid + 1, high);
}
else
return -1;
}
49 changes: 49 additions & 0 deletions ps1/p2/binarysearch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* tab:2
*
* search.h - Search function & helper function declarations
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* IN NO EVENT SHALL THE AUTHOR OR THE UNIVERSITY OF ILLINOIS BE LIABLE TO
* ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
* DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
* EVEN IF THE AUTHOR AND/OR THE UNIVERSITY OF ILLINOIS HAS BEEN ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE AUTHOR AND THE UNIVERSITY OF ILLINOIS SPECIFICALLY DISCLAIM ANY
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
* PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND NEITHER THE AUTHOR NOR
* THE UNIVERSITY OF ILLINOIS HAS ANY OBLIGATION TO PROVIDE MAINTENANCE,
* SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
* Author: Aamir Hasan
* Version: 1
* Creation Date: Fri Aug 30 2020
* Filename: search.c
* History:
* AS 1 Fri Aug 30 2020
* First written.
*/

#ifndef BINARYSEARCH_H
#define BINARYSEARCH_H

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

// binarySearch
// searches for an element in the BST recursively
// Returns:
// 1 - found element
// -1 - element not found
extern int32_t binarySearch(int32_t* arr, int32_t md, int32_t low, int32_t high);

extern int32_t binarySearch_asm(int32_t* arr, int32_t md, int32_t low, int32_t high);

#endif
116 changes: 116 additions & 0 deletions ps1/p2/binarysearch_asm.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* tab:2
*
* search_asm.S - Implementation of Assembly Recursive DFS
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* IN NO EVENT SHALL THE AUTHOR OR THE UNIVERSITY OF ILLINOIS BE LIABLE TO
* ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
* DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
* EVEN IF THE AUTHOR AND/OR THE UNIVERSITY OF ILLINOIS HAS BEEN ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE AUTHOR AND THE UNIVERSITY OF ILLINOIS SPECIFICALLY DISCLAIM ANY
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
* PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND NEITHER THE AUTHOR NOR
* THE UNIVERSITY OF ILLINOIS HAS ANY OBLIGATION TO PROVIDE MAINTENANCE,
* SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
* Author: Aamir Hasan
* Version: 1
* Creation Date: Fri Aug 30 2020
* Filename: search_asm.S
* History:
* AS 1 Fri Aug 30 2020
* First written.
*/

.global binarySearch_asm

# Search ASM (Assembly)
# Searches for an element in a BST
# Declaration is in search.h
#
# Registers:
# eax - Return Value
#
binarySearch_asm:
pushl %ebp
movl %esp, %ebp

#--- YOUR CODE HERE ---
subl $4, %esp # reserve 4 bytes on stack for 1 local vars called mid
pushl %edi
pushl %esi
pushl %ecx
pushl %ebx

movl 8(%ebp), %ebx # load arr into EBX
movl 12(%ebp), %ecx # load md into ECX
movl 16(%ebp), %esi # load low into ESI
movl 20(%ebp), %edi # load high into EDI

cmpl %edi, %esi # load low - high into flag
ja not_exist # if low > high return -1

movl $0, %eax # set eax to 0
addl %esi,%eax
addl %edi, %eax # low + high -> eax
shrl %eax # eax = (low+high)/2

cmpl %ecx,(%ebx,%eax,4) # arr[mid] - md
je found
ja mid_larger_than_md
movl %eax, %esi # mid -> esi
incl %esi # new lower bound is mid+1

pushl %edi
pushl %esi
pushl %ecx
pushl %ebx
call binarySearch_asm
popl %ebx
popl %ecx
popl %esi
popl %edi



mid_larger_than_md:
movl %eax, %edi
decl %edi

pushl %edi
pushl %esi
pushl %ecx
pushl %ebx
call binarySearch_asm
popl %ebx
popl %ecx
popl %esi
popl %edi

found:
movl $1, %eax
jmp finish_search


not_exist:
movl $-1, %eax

finish_search:
popl %ebx
popl %ecx
popl %esi
popl %edi
addl $4, %esp

#----------------------

leave
ret
1 change: 1 addition & 0 deletions ps1/p2/grade.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10/10
16 changes: 16 additions & 0 deletions ps1/p2/input_large.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
15 1
13
3
4
12
14
10
5
1
8
2
7
9
11
6
18
8 changes: 8 additions & 0 deletions ps1/p2/input_small.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
7 20
50
30
20
40
70
60
80
Loading

0 comments on commit 6c083f5

Please sign in to comment.