代做Lab 6 Structs、代写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<<b
• 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 = ...
", op->a, op->printable, op->b);
int rval = table[op->operation](op->a, op->b);
op->result = rval;
printf("...%d %c %d = %d
", 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 WX:codinghelp
- 山西木业行业网:传承与创新,共筑绿色木业未来
- WhatsApp代拉群营销战略,这款WhatsApp营销工具让我与客户建立深厚的情感纽带
- 纸飞机群发拉群自动化软件,Telegram精准定位采集工具/TG营销神器
- 海外销售专家提问 WhatsApp拉群营销工具真的是业务成功的黄金钥匙吗
- TG-WS-LINE频道号,直登号,协议号,老号,怎么识别可靠的代筛全球app机构
- 外贸小白迷茫中 WhatsApp拉群营销工具有什么好用的 值得推荐吗
- 喊出要借助AI大模型重塑旗下所有业务的百度,到了检验成果的时候
- instagram一键爆粉群发营销工具,ins自动上粉推广引流软件
- 常州威雅学校:剑桥国际教师专业发展项目顺利开展!
- Ins/Instagram营销采集机器人,ins博主粉丝精准采集利器全新登场!
- 孟加拉#Telegram协议号-telegram劫持号-稳定耐用欢迎各大实力中介
- instagram低成本引流推广!Ins自动私信工具,Instagram营销软件推荐!
- 海纳AI面试官发布智能校招一体化解决方案
- 星际商海的新星探:2024年是否有跨境电商 Telegram 群发云控冒险者成为星际商海的新星探,勘探新商机
- 跨境运营的力量:WhatsApp代筛料子助您打造高质量数据库
- Ins群发群控工具,Instagram多开云控群发,助你实现营销目标!
- 情感共振 WhatsApp拉群营销工具用心打造每个用户的个性化体验之旅
- Instagram群发消息工具,Ins模拟器群发软件,助你快速营销!
- SLB与Equinor合作实现迄今为止最自主钻井
- instagram自动引流新招,ins一键打粉推广软件推荐
推荐
- 升级的脉脉,正在以招聘业务铺开商业化版图 长久以来,求职信息流不对称、单向的信息传递 科技
- 丰田章男称未来依然需要内燃机 已经启动电动机新项目 尽管电动车在全球范围内持续崛起,但丰田章男 科技
- 老杨第一次再度抓握住一瓶水,他由此产生了新的憧憬 瘫痪十四年后,老杨第一次再度抓握住一瓶水,他 科技
- 智慧驱动 共创未来| 东芝硬盘创新数据存储技术 为期三天的第五届中国(昆明)南亚社会公共安 科技
- 疫情期间 这个品牌实现了疯狂扩张 记得第一次喝瑞幸,还是2017年底去北京出差的 科技
- 创意驱动增长,Adobe护城河够深吗? Adobe通过其Creative Cloud订阅捆绑包具有 科技
- B站更新决策机构名单:共有 29 名掌权管理者,包括陈睿、徐逸、李旎、樊欣等人 1 月 15 日消息,据界面新闻,B站上周发布内部 科技
- 全力打造中国“创业之都”名片,第十届中国创业者大会将在郑州召开 北京创业科创科技中心主办的第十届中国创业 科技
- 如何经营一家好企业,需要具备什么要素特点 我们大多数人刚开始创办一家企业都遇到经营 科技
- 苹果罕见大降价,华为的压力给到了? 1、苹果官网罕见大降价冲上热搜。原因是苹 科技