首页
网站开发
桌面应用
管理软件
微信开发
App开发
嵌入式软件
工具软件
数据采集与分析
其他
首页
>
> 详细
代做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
热点项目
更多
代做ceng0013 design of a pro...
2024-11-13
代做mech4880 refrigeration a...
2024-11-13
代做mcd1350: media studies a...
2024-11-13
代写fint b338f (autumn 2024)...
2024-11-13
代做engd3000 design of tunab...
2024-11-13
代做n1611 financial economet...
2024-11-13
代做econ 2331: economic and ...
2024-11-13
代做cs770/870 assignment 8代...
2024-11-13
代写amath 481/581 autumn qua...
2024-11-13
代做ccc8013 the process of s...
2024-11-13
代写csit040 – modern comput...
2024-11-13
代写econ 2070: introduc2on t...
2024-11-13
代写cct260, project 2 person...
2024-11-13
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 9951568
© 2021
www.rj363.com
软件定制开发网!