<<

Example Solutions for Exam DD1377 Low Level Programming and Computer Architecture Exam date: 14:00-18:00, May 31, 2016

Examination of basic understanding (grades -E)

1 Errata: The name of the function is the same as the name of one of its parameters. During the exam students were instructed to rename the function to F .

2

2a Errata: Students were told during the exam to view the picture clicked as a link of a web page.

1. The control unit controls and monitors the execution of the computer, i.e., it ensures that instructions are decoded correctly to allow the execution of the right instruction. 2. The arithmetic and logic unit performs arithmetic operations on strings of bits viewed as words. This allows manipulation of information. 3. The memory allows the CPU to store and retrieve information. This is what makes it more powerful than a finite automata. 4. A peripheral device allows interaction with the computer. Examples include: key- board for entering text, speakers for listening to output, and touch pads for entering positional data and events tied to these.

Page 1 (of 6)

DD1377 Low Level Programming and Computer Architecture • Spring 2016 Douglas Wikström 2b

2c When the button is pressed an electrical pulse is generated to the USB socket. The socket is mounted on an IO card (that may be integrated on the mother board). This in turn generates an interrupt. The interrupt handler of the OS generates an event to the desktop (or other IO framework) which may capture it or forward it to the application with focus. The application is handed the position of the mouse pointer as part of the event. Then the application invokes the network stack to fetch the web page represented by the link. The network stack takes the URL, recovers the IP address, and waits for a response. The application then displays the downloaded web page.

3

3a The signs of a and b are −1 and 1 respectively. This follows since a char is stored in two’s complement representation and the first bit of the former variable is set, and the first bit of the second is not.

3b 0xb7, 0x01, 0xc1, 0x1a,

3c

loadc r1 0x83 // a loadc r2 0x35 // b or r3 r1 r2 // c and r4 r1 r2 // d loadc r7 1 // 1 shift r5 r1 r7 // e shift r6 r2 r7 // f

4 loadr r2 r1 // 3021 add r5 r5 r2 // 7552 addc r1 2 // 8102 jumpn r1 0x18 // f119

Page 2 (of 6)

DD1377 Low Level Programming and Computer Architecture • Spring 2016 Douglas Wikström 5

5a The instruction size, i.e., instruction alignment, determines the size of the increment.

5b jump, jumpe, jumpn, and jumple, and depending on interpretation halt.

6 This problem considers function call stacks. Recall that they are based on stacks implemented using an array with a pointer to the top element of the stack.

6a A call stack is needed to provide local for recursively called functions. Local scoping is important to provide encapsulation and reduce the risk of bugs in the . It is a very fast way of allocating memory when the memory requirements of a function is known at compile time.

6b A stack frame can contain: parameters, local variables, return address, and return value.

6c When a function call is made the stack pointer is incremented to make room for the stack frame, the parameters and return address are copied into the stack frame, and the is set to the start of the function that is called in memory. (In reality, some of these things take place in registers.) During the execution of the function the values of parameters and local variables may change. When the function returns the return value is stored in the stack frame, the stack pointer is decreased to pop the entire stack frame, and the program counter is set to the address following the instruction that made the function call.

7

7a

• CPP is the C pre-processor. It replaces macros by their verbatim definitions, i.e., #define. It uses pre-processor directives such as #include to manipulate which source files are given as output. • Gnu Assembler is used to translate an assembler program into an object file. The object file is almost an executable file, but certain labels have not been resolved into memory addresses and calls to functions have likewise not been resolved. • The Gnu takes object files as input, resolves their dependencies, and outputs an executable binary. The binary can either contain all dependencies, or they can be shared with other binaries. • The Gnu Makefile is used to automate the building of programs. Each file to be produced is identified by a target with certain dependencies. The command make reads the Makefile, resolves the dependencies, and executes separate tools such as the , the assembler, and the linker. This command avoids unnecessary work by keeping track of which files have changed and dependencies.

Page 3 (of 6)

DD1377 Low Level Programming and Computer Architecture • Spring 2016 Douglas Wikström 7b A software can consist of both C code and assembler code. The latter is used either for speed or for low-level access to hardware. A programmer converts the assembler code into object files using as. The C files are first combined and modified by the pre-processor cpp and then they are fed into the compiler gcc. The latter automatically invokes the former, so the former is rarely used explicitly. Finally, the linker ln combines the resulting object files into an executable binary. To avoid repeating the procedure manually, a Makefile is created and the command make is used to automate this.

Examination for higher grades (grades A-B)

8 Consider the function A(a2, a1, a0) = (b1, b0) defined by the following. a2 a1 a0 b1 b0 0 0 0 0 0 0 0 1 0 1 0 1 0 0 1 0 1 1 1 0 1 0 0 0 1 1 0 1 1 0 1 1 0 1 0 1 1 1 1 1

8a

8b The right-most two columns contains the row-wise sums of the three columns to the left.

8c

Page 4 (of 6)

DD1377 Low Level Programming and Computer Architecture • Spring 2016 Douglas Wikström 8d Exploit that the integers are represented in two’s complement. This means that we can implement a circuit that negates the second input before addition and re-use our addition circuit.

9

9a

typedef struct StackCell StackCell; struct StackCell { int a; StackCell *b; }; struct Stack { StackCell *c; }; typedef struct Stack Stack;

void push(Stack *d, int a); int pop(Stack *d);

9b StackCell declares a container that stores an int and a pointer to another stack cell. This allows forming a chain of stack cells, where each stack cell holds a value on the stack and the bottom of the stack is the stack cell whose pointer does not point to another stack cell. It points to NULL instead. Stack is simply a holder of a pointer to the top-most stack cell. push takes a pointer to a stack and an int value as input and stores the value in a new stack cell that points to the previously top-most stack cell (or NULL if the stack was empty). pop returns the value of the top-most stack cell and copies its pointer to the stack. The former function allocates space for the new stack cell on the heap and the latter releases it.

9c

void push(Stack *stack, int data) { StackCell *stackCell = malloc(sizeof(*stackCell)); stackCell->data = data; stackCell->next = stack->top; stack->top = stackCell; }

Page 5 (of 6)

DD1377 Low Level Programming and Computer Architecture • Spring 2016 Douglas Wikström 9d

int pop(Stack *stack) { StackCell *stackCell = (*stack).top; if (stackCell != 0) { stack->top = stack->top->next; int data = stackCell->data; free(stackCell); return data; } else { return -1; } }

Page 6 (of 6)

DD1377 Low Level Programming and Computer Architecture • Spring 2016 Douglas Wikström