首页 > > 详细

代做program、代写c/c++编程设计

项目预算:   开发周期:  发布时间:   要求地区:
Lab 6 Structs, Function Pointers, Multiply & Divide
Contents
Lab 6 Structs, Function Pointers, Multiply & Divide .....................................................................................1
Version 1 .......................................................................................................................................................2
Dates.............................................................................................................................................................2
Early by 11:58 PM, Thursday 11-April-2024 .........................................................................................2
On Time by 11:58 PM, Friday 12-April-2024.........................................................................................2
Late until 11:58 PM, Saturday 13-April-2024........................................................................................2
Introduction ..................................................................................................................................................2
The Struct and the Jump Table .....................................................................................................................2
The ops.s file .................................................................................................................................................3
4 Functions................................................................................................................................................3
Plus............................................................................................................................................................3
Left Shift....................................................................................................................................................3
Times.........................................................................................................................................................3
Divide ........................................................................................................................................................3
Warp-up for ops........................................................................................................................................3
The do_op.s file.............................................................................................................................................3
Shims.............................................................................................................................................................4
Output...........................................................................................................................................................4
ops_test ....................................................................................................................................................4
lab6 ...........................................................................................................................................................5
bonus ........................................................................................................................................................5
Bonus +2 points – do not try until you have working code turned in ..........................................................5
Register Allocation........................................................................................................................................6
Required Stuff – There’s new stuff here.......................................................................................................6
Comments.....................................................................................................................................................6
Readme .........................................................................................................................................................6
Submission....................................................................................................................................................7
Machine Generated Code.............................................................................................................................7
Version 1
Dates
Early by 11:58 PM, Thursday 11-April-2024
On Time by 11:58 PM, Friday 12-April-2024
Late until 11:58 PM, Saturday 13-April-2024
Introduction
This lab involves:
• Function pointers in assembler
• Structs
• Shift by a variable amount
• Divide
• Multiply
You will write two files of assembler code, ops.s and do_op.s for this lab. For the bonus, you would
write search.s as well. The supplied zipfile has everything else you need, but the makefile will have to be
customized with your zip target and your name.
The Struct and the Jump Table
The following code is in lab6.c:
struct Operation {
int result; // offset is zero
char printable; // offset is 4
short operation; // offset is 6
int a,b; // offsets are 8 and 12
}; // the struct is 16B long
typedef int (*mathOp)(int a, int b);
mathOp table[] = { plus, left_shift, times, divide};
struct Operation ops_table[] = {
{0, '+', 0, 4, 16},
{0, '<', 1, 16, 4},
{0, '*', 2, 16, 4},
{0, '/', 3, 16, 4}
};
The ops.s file
4 Functions
This file will contain 4 very short functions (plus, left_shift, times, and divide). They will return the
values listed below
• a+b
• a<• a*b
• a/b
Plus
The plus function is extremely simple, get the sum of %rdi and %rsi into %rax.
Left Shift
To left shift by a variable amount, the number of places to shift needs to wind up in %cl. Put the value
you want shifted in eax and then use (shift, arithmetic, left, long)
sall %cl, %eax
Times
For times, use imul. This version of multiply is pretty straightforward.
Divide
Because of the way the original chip was severely limited, divide has very particular idiosyncrasies. The
thing you will divide starts out in RAX or a smaller version of it. Then we do magic on it to extend it,
which will trash RDX (or a smaller subset of it). Then we can divide by any of the other registers.
Assume that our multiply was on assembler longs and not quads, which means EAX holds our result. If
ECX holds the value we are dividing by, we would get the code below. It can be in any of the caller saved
other than EAX or EDX. That would yield:
cltd # the magic conversion that trashes edx
idivl %ecx # divide eax by ecx, result is in eax
Warp-up for ops
That’s it. 4 really short functions that only use the parameter registers and rax. Use the ops_test target
to test them. Write them one at a time and comment out ones you don’t want tested. (Read the
supplied code in ops_test.c. The ops_test code uses a special shim to make sure that your 4 function
don’t mess up,.
Even if you have no clue about structs, you can start on these four right away.
The do_op.s file
The do_op function goes in this file. It feeds a couple of pesky prints and also calls the appropriate math
function. It does not call them directly. (Go read the supplied lab6.c file). The function gets passed:
• A pointer to a struct Operation
• A table of function pointers
This is what the code should do:
int do_op( struct Operation *op, mathOp table[])
{
printf("%d %c %d = ...\n", op->a, op->printable, op->b);
int rval = table[op->operation](op->a, op->b);
op->result = rval;
printf("...%d %c %d = %d\n\n", op->a, op->printable, op->b,
op->result);
return rval;
}
1. First is stack housekeeping – this function makes many calls, you know what registers it needs
2. Next, deal with print (your code will call print, not printf). Grab the parameters that print needs
from memory. Use the offsets to figure where to get the values. Be very careful about sizes.
Remember that printf wants anything smaller than an int to be promoted to an int.
3. Next is the indirect call. Get the operation member out of the struct, converting the C short to a
C long (assembler quad) so that you can use it to get into the array of pointers. Then get the
two parameters out of the struct and into the correct parameter registers. Then make the
indirect call. Be sure to stash the value returned someplace safe.
4. Then deal with the second printf (your code will call print), which should be easy since it is very
much like the first one.
5. Lastly, deal with he return value and unwinding the stack.
There are only 2 complicated parts here, dealing with struct members and the indirect call. A glance at
the favorite opcode slide deck might be in order, it shows how to make indirect calls. You have an array
of 8-byte pointers in memory, one of them is the correct one to call.
Shims
The driver code deals with most of the shims. Your code has to call print and not printf. Your lab 6
won’t have a shim between do_op and the math operation functions it calls, but ops_test will have a
shim making sure that the math operation functions are clean.
Output
Your output should match these.
ops_test
[kirby.249@cse-sl2 lab6]$ ops_test
16 + 4 = ...
...16 + 4 = 20
16 < 4 = ...
...16 < 4 = 256
16 * 4 = ...
...16 * 4 = 64
16 / 4 = ...
...16 / 4 = 4
[kirby.249@cse-sl2 lab6]$
lab6
[kirby.249@cse-sl2 lab6]$ lab6
Lab 6 is searching...
16 / 4 = ...
...16 / 4 = 4
16 * 4 = ...
...16 * 4 = 64
16 < 4 = ...
...16 < 4 = 256
4 + 16 = ...
...4 + 16 = 20
The largest result comes from: 16 < 4 = 256
[kirby.249@cse-sl2 lab6]$
bonus
BONUS is searching...
16 / 4 = ...
...16 / 4 = 4
16 * 4 = ...
...16 * 4 = 64
16 < 4 = ...
...16 < 4 = 256
4 + 16 = ...
...4 + 16 = 20
The largest result comes from: 16 < 4 = 256
[kirby.249@cse-sl2 lab6]$
Bonus +2 points – do not try until you have working code turned in
Do search as an assembler function and put it in a file called “search.s” as seen in the makefile for the
bonus target. Use the provided C code to see what search has to do.
Register Allocation
Register allocation policy is graded.
SOme of the required functions are leaf-level and the some are not. This guides your register allocation
policy.
You really do want a firm grasp on register allocation before you take the final exam, so work it out here.
Before you write any code, figure out how many “variables” you will need and assign them to registers
of the appropriate type.
Required Stuff – There’s new stuff here
All of your functions must create a stack frame upon entry and tear it down upon exit.
Different from lab 5:
Near the top of every function, create in comments your pocket guide to the registers. Each register
used in the function must have a comment telling what that register means. For example:
# rax is both a subscript and return value at the same time.
In your comments you should refer to the registers not by name but by what they mean.
Comments
Comment your code. Comments should say things that are not obvious from the code. In assembler
you could easily have something to say for every line of code. You could easily have more comment lines
than code lines. Comment what each register holds. Comment about how the operation has a higher
level meaning. Try to avid comments that say the exact same thing as the code – this might get you
points off.
Near the top of every function, write out in comments what every register the code uses will mean. This
is your pocket guide to the registers when writing the function.
Put your name and the assignment number in your initial comments. Also add the statement that says
that you wrote all of the code in the file (see below). Or you can forget it and get a zero on the lab.
Readme
As always, create a text README file, and submit it with your code. All labs require a readme file.
Include:
• Your name
• If it qualifies for bonus points
• Hours worked on the lab
• Short description of any concerns, interesting problems, or discoveries encountered. General
comments about the lab are welcome.
Submission
No surprises here. Your zip file needs:
• A readme file
• All of your .s files
• Makefile, modified to add a zip target and your name
• The supplied files needed to make all targets build
Be sure that the zip target in the makefile self-tests by making ops_test and lab6 (and possibly bonus)
from the contents of the zip file. Any zip file that fails to build gets a zero. Any makefile that doesn’t
self-test the zip is marked late regardless of when it was turned in.
Be sure to add this text to ALL of your .s files:
# BY SUBMITTING THIS FILE AS PART OF MY LAB ASSIGNMENT, I CERTIFY THAT
# ALL OF THE CODE FOUND WITHIN THIS FILE WAS CREATED BY ME WITH NO
# ASSISTANCE FROM ANY PERSON OTHER THAN THE INSTRUCTOR OF THIS COURSE
# OR ONE OF OUR UNDERGRADUATE GRADERS. I WROTE THIS CODE BY HAND,
# IT IS NOT MACHINE GENRATED OR TAKEN FROM MACHINE GENERATED CODE.
If you omit a required file, you get zero points.
If you fail to add the above comment you get zero points
If the make command as given generates any warnings or errors you get zero points
Every file you hand edit needs your name in it.
Machine Generated Code
Do not use –S with gcc to generate any assembler from C code. Do not use any AI such as github copilot
to do this lab. Doing so is academic conduct.

软件开发、广告设计客服
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 9951568
© 2021 www.rj363.com
软件定制开发网!