联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2022-06-18 08:10

C Programming Assignment

Due date: 24:00 12/06/2022

Total marks: 9

This assignment aims to give you some experience with C programming and to help you gain

better understanding of the instruction format of LC-3.

Important Notes:

? Markers will use instructions that are different from the examples given in the specifications

when testing your programs.

? The files containing the examples can be downloaded from course web site and unpacked

on a Unix machine with the command below:

o tar xvf examplefiles.tar.gz

2

Disassembler is a very useful reverse engineering tool1

. It converts machine instructions to

human-readable assembly language equivalent. Writing a disassembler helps you know how a

computer understands the meaning of a machine instruction. In this assignment, you are

required to implement a disassembler for a subset of the LC-3 assembly language.

Part 1 (4 marks)

In this part of the assignment, you are required to write a C program to convert the machine

code that correspond to LC-3’s ADD and AND instruction to assembly language instructions. The

detailed requirements are as below:

1. Each machine instruction is represented as a four-digit hexadecimal number. All the

instructions are stored in a file.

2. Each line of the file stores exactly one instruction. For example:

5105

0ffd

To make the generation of the file easier, it should be assumed that the file is a text file

and each four-digit hexadecimal number is stored as a string of characters.

Function unsigned long int strtoul(const char *str, char **endptr, int base) declared in

stdlib.h converts the initial part of the string in str to an unsigned long int value

according to the given base.

3. For this part, it should be assumed that the operands only use the “register” addressing

mode. That is, the values of all the operands are stored in registers.

4. Your program should read each of the instructions in the file and convert each

instruction to an LC-3 assembly language instruction. The results should be displayed on

the screen.

5. The exact name of the file that contains the machine code instructions must be given as

a command line argument.

6. Name this program as part1.c

An example is given below. In this example, the name of the file that contains the machine

code instructions is “obj” (NOTE: “obj” is the exact name of the file. It does NOT have any

suffix.). Markers might use a file with a different name when testing your program. For this

example, the contents of “obj” are:

1283

5105

The execution of the program is as below. The outputs of the program are marked in blue.

$ ./part1 obj1

add r1,r2,r3

1 Have you ever wondered how a hacker figures out the ways to circumvent the anti-piracy defence

measure of software? Disassembler is one of the tools that helps a hacker to understand how a software

works.

3

and r0,r4,r5

Part 2 (1 marks)

Expand your program in Part 1 to allow the operand to use the “immediate” addressing mode.

That is, the value of an operand is stored in the instruction. In the result, the value operand

should be displayed as a decimal number.

Name this program as part2.c

An example is given below. In this example, the name of the file that contains the machine code

instructions is “obj” (NOTE: “obj” is the exact name of the file. It does NOT have any suffix.).

Markers might use a file with a different name when testing your program. For this example, the

contents of “obj” are:

1283

5105

1df7

506f

The execution of the program is as below. The outputs of the program are marked in blue.

$ ./part2 obj2

add r1,r2,r3

and r0,r4,r5

add r6,r7,-9

and r0,r1,15

Part 3 (2 marks)

Expand your program in Part 2 to include instruction JMP.

Name this program as part3.c

An example is given below. In this example, the name of the file that contains the machine code

instructions is “obj” (NOTE: “obj” is the exact name of the file. It does NOT have any suffix.).

Markers might use a file with a different name when testing your program. For this example, the

contents of “obj” are:

1283

5105

1df7

506f

c080

4

The execution of the program is as below. The outputs of the program are marked in blue.

$ ./part3 obj3

add r1,r2,r3

and r0,r4,r5。、

add r6,r7,-9

and r0,r1,15

jmp r2

Part 4 (2 marks)

1. Expand your program in Part 3 to include instruction BR.

2. The starting address of the program must be given as another command line argument

that follows the name of the file containing the machine code. It should be assumed

that the address is a 4-digit hexadecimal number.

3. The address of the instruction to be branched to should be displayed as a 4-digit

hexadecimal number with prefix “0x”.

4. Name this program as part4.c

An example is given below. In this example, the name of the file that contains the machine code

instructions is “obj” (NOTE: “obj” is the exact name of the file. It does NOT have any suffix.).

Markers might use a file with a different name when testing your program. For this example, the

contents of “obj” are:

1283

5105

0ffd

0802

1df7

506f

c080

The execution of the program is as below. The outputs of the program are marked in blue.

$ ./part4 obj 3000

add r1,r2,r3

and r0,r4,r5

brnzp 0x3000

brn 0x3006

add r6,r7,-9

and r0,r1,15

jmp r2

5

Submission

1. Use command “tar cvzf asg.tar.gz part1.c part2.c part3.c part4.c” to pack the four C

programs to file asg.tar.gz.

2. Submit asg.tar.gz.

Debugging Tips

1. Debugging is a skill that you are expected to acquire. Once you start working, you are paid

to write and debug programs. Nobody is going to help you with debugging. So, you should

acquire the skill now. You can only acquire it by practicing.

2. If you get a “segmentation faults” while running a program, the best way to locate the

statement that causes the bug is to insert “printf” into your program.

3. If you can see the output of the “printf” statement, it means the bug is caused by a

statement that appears somewhere after the “printf” statement. In this case, you should

move the “printf” statement forward. Repeat this process until you cannot see the output of

the “printf” statement.

4. If you cannot see the output of the “printf” statement, it means the bug is caused by a

statement that appears somewhere before the “printf” statement.

5. Combining step 3 and 4, you should be able to identify the statement that causes the

“segmentation faults”.

6. Once you identify the statement that causes the “segmentation faults”, you can analyse the

cause of bug, e.g., whether the variables have the expected values.

Suggestions

? You should create more test cases to test your program. The easiest way is to use LC-3

assembler and simulator.

? You can use a lot of C library functions. You might want to consider using the functions

defined in “string.h” as it has a lot of functions for manipulating strings

Further Work (no marks)

Implement a disassembler that is capable of decoding the full set of LC-3’s instructions.


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp