Yuxi Chen CS250 homework 1. Implement the boolean function for segment b for a 7-segment decoder. Using four switches w, x, y, z choose the digits 0, 1, 2, 3,4,5,6,7,8,9,A,B,C,D,E,F in the seven segment display. a) Write the truth table for the segment b w x y z b 0 0 0 0 1 0 0 0 1 1 0 0 1 0 1 0 0 1 1 1 0 1 0 0 1 0 1 0 1 0 0 1 1 0 0 0 1 1 1 1 1 0 0 0 1 1 0 0 1 1 1 0 1 0 1 1 0 1 1 0 1 1 0 0 0 1 1 0 1 1 1 1 1 0 0 1 1 1 1 0 b) Write the Karnaugh maps for b wx\yz 00 01 11 10 00 1 1 1 1 01 1 0 1 0 11 0 1 0 0 10 1 1 0 1 c) Using the Karnaugh maps, write the boolean expressions for segment a and b using a minimum "sum of products" boolean expression. b = w'x'+x'z'+w'y'z'+wy'z+w'yz d) Draw the schematic of your implementation using NAND gates.
2. Draw a Flip Flop a) Using NAND gates b) Using NOR gates 3. In each line write the name of the memory section where the variables are stored: int a = 5; // a is stored in data int b[20]; // b is stored in bss int main() { // main is stored in text { int x; // x is stored in stack int *p = (int *)malloc(sizeof(int)); // p points to memory stored in heap
4. Enumerate the steps needed to load a program. 1. The loader is a program that is used to run an executable file in a process. 2. Before the program starts running, the loader allocates space for all the sections of the executable file (text, data, bss etc) 3. It loads into memory the executable and shared libraries (if not loaded yet) 4. It also writes (resolves) any values in the executable to point to the functions/variables in the shared libraries.(e.g. calls to printf in hello.c) 5. Once memory image is ready, the loader jumps to the _start entry point that calls init() of all libraries and initializes static constructors. Then it calls main() and the program begins. 6. _start also calls exit() when main() returns. 5. What is a "Dynamic Linker" and a "Static Linker". A Dynamic Linker is the part of an operating system (OS) that loads and links the shared libraries for an executable when it is executed. A Static Linker puts together all object files as well as the object files in static libraries 6. Perform the following multiplication in binary. Also, show convert to decimal the operands and the result. 1001010 x 1101 1001010 * 1101 1001010 1001010 1001010 1111000010 B1001010 = 2^1+2^3+2^6 = 74 B1101 = 2^0+2^2+2^4 = 13 B1111000010 = 2^1+2^6+2^7+2^8+2^9 = 96
7. Perform the following division in binary, Also, convert to decimal the operands and the result. 1 100101001 1111 10000 0 B100101001 = 2^0+2^3+2^5+2^8 = 297 B = 2^0+2^1+2^3 = 11 B1 = 2^0+2^1+2^3+2^4 = 27 8. Represent the number1.25 in binary using the IEEE 754 double representation. The formula is given: Val in decimal = (-1) s x (1.m) x 2 (e-bias) where: bias = 1023 s = bit 63 e = bits 52 to 62 m= bits 0 to 51 Val in binary: 0011 1111 1110 0100 0000 0000 0000 0000
9. Explain what is a "Pipe Stall" and how can be avoided. Pipe Stall happens if the next instruction depends on the result of the previous instruction. To avoid stalls: Reorder the instructions Avoid introducing unnecessary branches Delay references to result register(s) 10. Obtain the representation of number -58 using 8 bits. Verify that 78 + (-58) is equal to 20 using binary addition and complements of 2. -58: 11000110 + 78: 01001110 20: 00010100 11. Write a function "int isbigendian()" that will return 1 if the computer is big endian or 0 if it is little endian. int isbigendian(){ int test=5; if (*(char*)&test==5) { return 0; else return 1; 12. What is the difference between Harvard Architecture and Von Newman Architecture In Harvard Architecture code and data are stored in separate memory, while in Von Newman Architecture code and data are stored together in memory. 13. What do CISC and RISC stand for and enumerate the characteristics of each. CISC: Complex Instruction Set Computer 1. It contains many instructions, often hundreds. 2. Some instructions take longer than others to complete RISC: Reduced Instruction Set Computer 1. It contains few instructions 32 or 64 2. Instructions have a fixed length 3. Each instruction is executed in one clock cycle.
14. Write the following characteristics of the ATMEGA328: Program Memory (KB): 32 RAM (KB): 2 Clock Speed (Hz): 20M Number of I/O ports: 23 Number of A/D ports: 6 14. Rewrite the following program in AVR assembly language. Remember that int's in AVR are 2 bytes long. int a = 6; int b = 3; int c; void setup() { c = a + b; loop() { setup: lds r24,b lds r25,(b)+1 lds r18,a lds r19,(a)+1 add r24,r18 adc r25,r19 sts (c)+1,r25 sts c,r24 ret 15. Rewrite the following program in AVR assembly language. Remember that int's in AVR are 2 bytes long. int a = 6; int b = 4; int c; void setup() { c = a * b; loop() {
setup: lds r20,b lds r21,(b)+1 lds r18,a lds r19,(a)+1 mul r20,r18 movw r24,r0 mul r20,r19 add r25,r0 mul r21,r18 add r25,r0 clr r1 sts (c)+1,r25 sts c,r24 ret 16. In x86-64 Assembly language implement the function int addarray(int n, int * array) that add all the elements of the array passed as parameter. n is the length of the array..text.globl addarray.type addarray, @function # int addarray(int n, int * array) { # // n = %rdi array = %rsi addarray: # // i = %rdx sum = %rax # movq $0,%rdx # i=0 ; movl $0,%eax # sum=0 ; # while: cmpq %rdx,%rdi # while (i<n) { // (n-i>0) jle afterw # # addl (%rsi),%eax # sum += *array; addq $1,%rdx # i++ ; addq $4,%rsi # array++ ; jmp while # afterw: ret # return sum; 17. In x86-64 Assembly language implement a program "maxmin" that prompts two integer numbers fom stdin and then displays the maximum, minimum and average of both: numbers. Here is an example of the usage: bash> maxmin a=5 b=8 max=8 min=5 avg=6
#maxmin.s.data.comm a,8.comm b,8 str1:.string str2:.string str3:.string str4:.string.text.globl main.type main: str5:.string "a=" "b=" "%d" "max=%d\n" "min=%d\n" main, @function movq $str1, %rdi call printf movq $a, %rdx movq $str3, %rax movq %rdx, %rsi movq %rax, %rdi call scanf movq $str2, %rdi call printf movq $b, %rdx movq $str3, %rax movq %rdx, %rsi movq %rax, %rdi call scanf movq $a, %rdi movq $b, %rsi movq (%rdi), %rax movq (%rsi), %rdx cmpq %rdx,%rax jg tag movq $a, %rdi movq $b, %rsi movq (%rsi), %r8 movq (%rdi), %r9 movq %r9, (%rsi) movq %r8, (%rdi)
tag: movq $a, %rdx movq (%rdx), %rsi movq $str4, %rdi call printf movq $b, %rdx movq (%rdx), %rsi movq $str5, %rdi call printf ret