Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
45 views8 pages

Midex2023 Solutions

The document is a midterm exam for a computer science course. It contains 5 problems testing knowledge of computer architecture, assembly language, and C programming. The exam tests understanding of concepts like memory addressing, instruction decoding, condition codes, and binary operations.

Uploaded by

auguz2702
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views8 pages

Midex2023 Solutions

The document is a midterm exam for a computer science course. It contains 5 problems testing knowledge of computer architecture, assembly language, and C programming. The exam tests understanding of concepts like memory addressing, instruction decoding, condition codes, and binary operations.

Uploaded by

auguz2702
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Full name: Page 1 of 8

Full Name: Solutions

CSE 205, 2022-2023


Midterm Exam
29 March 2023

Instructions:

• Make sure that your exam is not missing any sheets, then write your full name on all the page
headers (in case sheets get garbled).

• Write your answers in the space provided below the problem. If you make a mess, clearly indicate
your final answer.

• You have 1.5 hours to solve the exam.

• The problems are of varying difficulty. Do not panic if you cannot answer one, just skip it and come
back if you have time.

• No notes except for a single A4 sheet (both sides, whatever font size you can read).

• Electronic devices are not permitted (no phones, no laptops, no ebooks, no calculators)

• Good luck!
Full name: Page 2 of 8

Problem 1.

1. You the type the following command into the shell

./script.sh

And receive the error message:

-bash: ./script.sh: Permission denied

What command(s) could you give to run this shell script?


chmod u+x script.sh
./script.sh
# Do NOT use sudo to execute shell scripts...

2. What does the following command print on the terminal?

if (exit 3); then echo "TRUE"; else echo "FALSE"; fi


FALSE

You type the following four commands into the shell. What does the last one display?

echo one > data.txt


echo two >> data.txt
echo three > data.txt
cat data.txt

three

You type the following four commands into the shell. What does the last one display?

mkdir test
NAME=Walter
echo 'My name is $NAME' > test/first.txt
echo "I am $((50 + 2))" > test/second.txt
for f in test/*.txt; do cat $f; done

My name is $NAME
I am 52
Full name: Page 3 of 8

Problem 2.
1. The xor gate, shown below at left, has the truth table shown below at right.
a b c
0 0 0
a c
xor 1 0 1
b
0 1 1
1 1 0
If you only have and and not gates, can you still implement a xor gate? If so, how?

(not (a and b)) and (not ((not a) and (not b)))

2. Complete the truth table for the following circuit.


a b c d
a xor c 0 0 0 0
0 1 1 0
b 1 0 1 0
and d
1 1 0 1

3. What function is implemented by the circuit above?


It's a 1-bit adder (half adder)

4. Draw a sequential logic circuit that takes an input x and produces two outputs. The first output is true
only if the current and previous value of x were both 1. The second output is true only if the current
and two previous values of x were both 1.

two consecutive
ones on x

three consecutive
ones on x
Full name: Page 4 of 8

Problem 3.
The diagram below shows part of the decode stage of a simple processor.

valP

icode ifun rA rB valC


PC
increment
Byte 0 Byte 1 Bytes 2 to 10

Instruction
memory

PC

Assume that the processor memory has been loaded from the object file shown below and that PC = 0x000.

0x000: 30f20a00000000000000 | irmovq $10,%rdx


0x00a: 30f00300000000000000 | irmovq $3,%rax
0x014: 10 | nop
0x015: 6020 | addq %rdx,%rax
0x017: 00 | halt

1. What are the values of the four wires leading into icode?
0011 (0x3)

2. What are the values of the four wires leading into ifun?

0000 (0x0)

3. What are the values of the four wires leading into rA?
1111 (0xF)

4. What are the values of the four wires leading into rB?
0010 (0x2)

5. What is the value of valP in hexadecimal?

0x00a

6. The processor clock ticks twice: what is the value of icode now?

0001 (0x1)
Full name: Page 5 of 8

Problem 4.

1. Consider the following C code snippets. Assume that a char is an unsigned 8-bit integer. Write the
hexadecimal representation of the value in the result variable after executing each snippet.

EXAMPLE) char a, result;


a = 0x1 + 0x1;
result = a | 0xF0;

/*
Value of result: 0xF2
*/
(a) char result = (0xCA & 0x31);

/*
Value of result: 0x00
*/
(b) char result = (˜ 0xA3) ˆ 0xC5;

/*
Value of result: 0x99
*/
(c) char a, b, c, result;
a = 0x5;
b = (a >> 2) << 1;
c = (b | a) << 2;
result = c & 0x38;

/*
Value of result: 0x18
*/
(d) char a, result;
a = (! 0x0) << (0x1 << 0x02);
result = ˜(0xFC + a);

/*
Value of result: 0xF3
*/
Full name: Page 6 of 8

2. Consider the following C code snippets. Assume an 8-bit arithmetic logic unit (ALU). Write the
condition code values after each operation.

0x11 + 0x01
/*
(a) ZF: 0 SF: 0 OF: 0 CF: 0
*/

0xFE + 0x02;
/*
(b) ZF: 1 SF: 0 OF: 0 CF: 1
*/

0xCC & 0xCC;


/*
(c) ZF: 0 SF: 1 OF: 0 CF: 0
*/

3. Write the decimal values represented by the following 8-bit signed integers whose bit patterns are
shown in hexadecimal.

a) 0x22
Decimal value: 34

b) 0x13
Decimal value: 19

c) 0xFD
Decimal value: -3

d) 0x84
Decimal value: -124
Full name: Page 7 of 8

Problem 5.

1. Consider the following Y86-64 assembly program.

irmovq $9, %r8


irmovq $2, %rax
subq %rax, %r8
rrmovq %r8, %rax
subq %rax, %rax
halt

You assemble the program with yas and then simulate it with yis. Fill in the missing output below
(including the condition codes).

Stopped in 6 steps at PC = 0x1a. 1 S=__


Status 'HLT', CC Z=__ 0
0 O=__
Changes to registers:
%r8: 0x0000000000000000 0x0000000000000007

Changes to memory:

2. What is the value of %rax after executing the following Y86-64 assembly program.

irmovq $3, %rax


irmovq $-8, %rdx
addq %rdx, %rax
jg L1
irmovq $7, %rax
7
jmp L2
L1: irmovq $42, %rax
L2: halt
Full name: Page 8 of 8

3. Consider the following Y86-64 assembly program.

irmovq y, %rdx
mrmovq -8(%rdx), %r10
mrmovq 8(%rdx), %r11
addq %r10, %r11
rmmovq %r11, (%rdx)
halt

.pos 0x100
x: .quad 1
y: .quad 2
z: .quad 3

After executing the program, what 64-bit values are stored in the memory at

(a) x? 1
(b) y? 4

(c) z? 3

4. What is the value in %rax after executing the following X86 64 assembly program?
(Recall that the meaning of D(Rb, Ri, S) is Mem[ Reg[Rb] + S * Reg[Ri] + D ] and
assume that all memory addresses initially contain zero.)

mov $0xf400, %rdx


mov $0x2, %rcx
movq $1, (%rdx, %rcx, 8)
movq $2, 8(%rdx, %rcx, 8)
movq $3, 0x10(%rdx, %rcx, 8)
movq $4, 0x20(%rdx, %rcx, 4)
2
mov $3, %rcx
mov (%rdx, %rcx, 8), %rax
hlt

You might also like