For the following segment, what is SIZEOF myChecker (in decimal - ignore the .0000 from Canvas) .data myChecker BYTE 12h BYTE 34h BYTE 56h BYTE 78h BYTE 90h The _________ operator returns a value that is equivalent to multiplying the number of elements in a single data declaration by the size, in bytes, of a single element of a data declaration. The _________ operator overrides the default type of a label (variable), and can also be used to combine elements of a smaller data type and move them into a larger operand. What do these do?
lodsb: load string byte True or False? If the string direction flag is not set, string operations will move backward through the string. False. It only moves backwards when the direction flag is set. Suppose that you are given the following partial data segment, which starts at address offset 0x1000 : x contains what value, in decimal? y contains what value, in decimal? Y contains 6. There are 6 elements in the array, so it has the lengthof 6. X contains 12, or the LENGTHOF the array * its TYPE size. Suppose that you are given the following partial data segment: .data myPtrCheck BYTE 12h, 34h, 56h, 78h, 90h, ABh, CDh, EFh .code ... mov eax, DWORD PTR [myPtrCheck+2] EAX contains what value, in hexadecimal? AB907856h It starts with 2 bytes beyond the first element of the array (on the third element). Although strings generally are stored sequentially, once it is stored as a single DWORD using PTR, then it becomes in reverse order. Assume that your
program has access to the following data segment (starting at address 0x310): What is the hexadecimal address of matrix[7][3] (the 4th element of the 8th row)? 3A6. This is a 50 x 10 matrix, with each row taking 210 bytes. Therefore, to figure out the address of the 8th row, add 7210 = 140. Next, we add 32 = 6, which will make 146 on top of the original address. Since the data segment starts at 310h, and we add 4 for the DWORD and 146 for the matrix navigation, we end up with 3A6. How would you write this as a postfix expression? 56 / (42 * 2.6 * 2) + (256 / (128 - 64)) * 3 ^ 12 56 42 2.6 * 2 * / 256 128 64 - / 3 12 ^ * + How would this postfix expression look when represented as infix? 3 3 * 5 4 2 * / - What FPU manipulations corresponds to the given infix notation? Z = (A+B-C)/D*E finit fld A fld B fadd fld C fsub fld D fdiv fld E fmul fstp Z How do you generate random numbers? First, call Randomize just once (e.g. main). Whenever you call RandomRange (N>0 in eax), it’ll return random number between 0 and N-1. Therefore, you need to find the range (hi-lo + 1), put that range in eax, and add that to the minimum. ;generate and display random integer n mov eax, N_MAX sub eax, N_MIN inc eax call RandomRange add eax, N_MIN mov esi, [ebp+12] mov [esi], eax call WriteDec call CrLF What specifically is indexed addressing? Indexed addressing: Used for global array references, array name with subscript [distance] Mov edi, 0 Mov list[edi], eax Add edi, 4 Mov list[edi], ebx What specifically is register indexed addressing? Register indirect addressing: Get the address of list into esi, and then use [esi]. Mov esi, [ebp+8] Mov eax, [esi] Add esi, 4 mov eax, [esi] What specifically is base-indexed addressing? Base-indexed addressing: Starting address in one register, offset in another, and add and access memory (used for referencing array elements in procedures). Mov edx, [ebp+8] Mov ecx, 20 Mov eax, [edx+ecx] Mov ebx, 4 Add eax, [edx+ebx] Mov [edx+ecx], eax Given the following register states, and using Indexed Addressing, which of the following lines of code will move the 11th element of the list array (of DWORDs) to the EAX register? EDX register contans the address of the first element of list. mov eax, list[ebx] In indexed addressing, you use the list and a subscript of the index distance you want. Because you want to add 10*4 to the original address, you want list[40]. Given the following register states, and using Register Indirect Addressing, which of the following lines of code will move the 11th element of the list array (of DWORDs) to the EAX register? EDX register contans the address of the first
element of list. mov eax, [esi] When you use register indirect addressing, you store the address of the element you want directly in a register, and then dereference that register to access the value. Given list, an array of WORDs, what element is addressed by list[8]? Hint: It’s Love. Given the following register states, and using Base Indexed Addressing, which of the following lines of code will move the 11th element of the list array (of DWORDs) to the EAX register? EDX register contans the address of the first element of list. mov eax, [edx + ebx] In base indexed addressing, there are two registers keeping track of two things: (1) the base address of the array and (2) the distance in bytes from the first element. Here, edx contains the addrss of the first element of the list, and ebx contain 40, the distance offset in bytes of the 11th element. The following instruction will increment the stack pointer (ESP) by how many bytes? ret 16 20 bytes. By default, it’ll also get rid of the return address (as a DWORD) when it goes back there. What would the stack look like (and at what distance from the ebp is everything)? .data .code main PROC push x push y push z call someProcedure ... exit main ENDP someProcedure PROC [ebp] - ebp [ebp+4] - @ret [ebp+8] - z - 90 [ebp+10] - y - 37 [ebp+12] - x - 153461 In essence, the distance from the top of the stack depends on the value closer to the stack (since it had decremented to get to that point). Therefore, 90 is at [ebp+8] even though it’s only a WORD because the stack pointer decremented down to [ebp+4] to accommodate the DWORD return address. Suppose that you are given the following program (with memory addresses shown on the left). .data .code main PROC 0x12 push x 0x17 mov AH, y 0x1C mov AL, z 0x21 call someProcedure 0x26 inc EAX 0x2B mov EBX, z 0x30 xor EAX, EBX 0x35 exit main ENDP END MAIN It should be 2B, the address of the very next instruction. Given the following partial data segment, what value would I put in the brackets in list[n] to access the 5th element of list? (Ignore the .0000 that Canvas may append to your answer). .MAX = 50 .data list DWORD MAX DUP(0) a DWORD 25 b DWORD 15
For this problem, suppose that you are working with the partial data segment given below. Assume that the memory address of balance is 0x44. What hexadecimal address belongs to the last item in history? HISTLIMIT = 100 .data balance DWORD 0 account WORD ? history WORD HISTLIMIT DUP(?) isValid BYTE 0 110h. 44h starts at decimal 68, and the first item in history will start 4+2 later, or at 74. The last item in history will be 99*2 more than that, which is 110h. Suppose that you are given the following MASM data segment declarations: .data idNumber BYTE ? status WORD 0 list DWORD 42 DUP(?) count DWORD ? The address of idNumber is 0x4E00. What is the hexadecimal address of the 14th element of list? What is the hexadecimal address of count? First, the starting address is 0x4E00, or 19968. Then, you add 1+2, or a starting address of 19971. list: 0x4E37. To find the 14th element, just add 134 = 20023, or 4E37h. Assume that LO and HI have already been assigned as constants with LO First, you can simply subtract directly to find HI-LO+1, and then add LO. mov eax,HI sub eax,LO inc eax call RandomRange add eax,LO mov x, eax Second, you can decrement LO, so that when you subtract it, you're actually getting HI-(LO-1), or HI-LO+1. mov eax,HI mov ebx,LO dec ebx sub eax,ebx call RandomRange add eax,LO mov x, eax main PROC push OFFSET result push x call factorial main ENDP factorial PROC push ebp mov ebp, esp mov eax, 1 mov ecx, *****A****** again: mul ecx loop again mov edi, ******B***** mov ******C*****, eax pop ebp ret *****D***** factorial ENDP A: {ebp+8]. Here, you want to use the actual value of x (since you’re multiplying). B: [ebp+12]. Here, you want to use the offset of result (b/c edi is normally for that). C: [edi]. In order to actually save the value in the address of result, you need to use edi as a register index. D: 8. Because you had pushed only two DWORDs, you only need to get rid of 2*4 = 8. What hexadecimal number will ESP contain when the “mov eax,1” instruction is executed? The initial address at the top of the stack is 0x0A50. main PROC push OFFSET result push x call factorial main ENDP factorial PROC Generally, you decrement every time you push something onto the stack. Therefore, since you’ve pushed 4 things onto the stack, you should add 4*4 = 12 to get to the original stack address (including ebp). Therefore, subtract 16 from 0A50h, and you’ll get 0A40. Given the following partial data segment, which starts at address 0x0200 .data
.data .code What does eax contain? 720, the 6th element of list. .data mov esi,
OFFSET list What does [esi] contain?
.data .code mov esi, OFFSET list mov ebx, y sub ebx, TYPE y add esi, ebx mov al, BYTE PTR [esi+1] The AL register contains \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ Again, we’re accessing the last element of the list. 40320 decimal = 0x9D80, or 00 00 9D 80 in big-endian. When you add 1 to the start of this element, you’re looking 1 byte in. In the little-endian IA-32, it’s 80 9D 00 00. So the 2nd byte is 0x9D = 157 Given the following partial data segment, which starts at address 0x0200 : .data matrix DWORD 20 DUP(5 DUP(?)) x DWORD LENGTHOF matrix y DWORD SIZEOF matrix pal BYTE ”Hello world.”,0 len DWORD LENGTHOF pal What does x contain? X contains 100, since that is literally how many elements long the array is. Y contains 400, since that is the LENGTHOF the array * TYPE. SInce the start of the address is 0x200 (512 in decimal), you just add the first 8 rows (854) and the next 2 elements (2*4) to get 512+168, or 02A8. .data .code What is being loaded where? 5678h is being loaded. PTR is almost always preceded by a TYPE so that you know what to store something as. Here, you’re storing a DWORD as a WORD, so you just look at the first two bytes. list DWORD 12345678h What is being loaded? mov al, BYTE PTR list mov al, BYTE PTR [list+1] mov al, BYTE PTR [list+2] mov ax, WORD PTR list mov ax, WORD PTR [list+2] In memory, this is stored as 78h, 56h, 34h, and 12h. mov al, BYTE PTR list: stores 78h into AL. mov al, BYTE PTR [list+1]: stores 56h into al. mov al, BYTE PTR [list+2]: stores 34h into al. mov ax, WORD PTR list: stores 5678h into AX. mov ax, WORD PTR [list+2] stores 1234h into AX. .data What is being loaded? First, everything in an array is stored in that order. Then, everything gets stored in reverse order in IA-32. mov ax, WORD PTR list: stores 3412h into ax. mov ax, WORD PTR [list+2]: stores 7856h into ax. mov eax, DWORD PTR list: stores 78563412h into eax. Given this code fragment, what is displayed? WriteChar displays the character in the AL register. pal BYTE ”Hello world.”,0 mov esi, OFFSET pal mov ecx, len sub ecx, 2 cld one: lodsb call WriteChar loop one mov ecx, len sub ecx, 2 std two: lodsb call WriteChar loop two First, the address of string “Hello World” is moved to esi, and its length moved to ecx (minus 2). Then, the direction flag is cleared (so it goes forward). One loads and writes the first element into AL, then loops until it’s written all but the null character and the period. Then, it puts len back into ecx (minus 2) and sets the reverse direction flag. Two loads and writes the last element, then decrements esi until it’s written all but the first letter. “Hello world.ldorw olle” What is the (string) result of adding the following digit string, digit by digit? a BYTE ”2458”,0 Literally, just convert each of the digits into their characters and add them up. 2(50) + 6(54) = 104 = h “hgei” How can you use ptr to contain the offset of another variable? .data .code The effect of this is the same as mov esi, OFFSET list. .data What is stored in [ptr]? NEVER try to deference [ptr]!! This is an invalid reference because this is a memory-to-memory location. Get the contents of ptr, and then get the contents of the address stored there. Instead, move the contents of ptr into another register, and then dereference that register. mov esi, ptr How could you calculate the sum of an integer array using register indirect addressing and a pointer? .data .code mov esi, ptrD mov ecx, LENGTHOF intList mov eax, 0 L1: add eax, [esi] add esi, TYPE intList loop L1 This loads the address of intList into esi, moves its length into ecx, and the accumulator into 0. Then, to add an integer, just dereference esi, adding the type of that list every time. What specifically do lodsb and stosb do? Lodsb moves byte at [esi] into the AL register. By contrast, stosb moves a byte from the AL register to the memory at [edi]. Both will increment esi/edi (respectively) if the direction flag is 0, and decrement if it equals 1. How do you write the ReadInt program? mov edx, OFFSET string mov ecx, MAXSIZE dec ecx call ReadString mov ecx, eax mov val, 0 mov esi, OFFSET string cld top: mov eax, 0 lodsb sub eax, 48 mov ebx, eax mov eax, val mov edx, 10 mul edx add eax, ebx mov val, eax loop top done: mov eax, val call WriteDec After ReadInt reads “5738” into val DWORD ?, what is stored in memory at val? 0X6A 0x16 0x0 0x0 Because 5738 is 166A in hex, this would normally be stored as 0000166A, but in IA-32 it is stored in reverse order. What is the binary tree method for converting from infix to postfix?
How is the FPU stack different? FPU is a “pushdown” stack. If you push more than 8 valus, the “bottom” of the stack will be lost. Registers may be referenced by name ST(x). It pushes ST(i) “down” to ST(i+1) for i = 0…6. It also loads ST(0) with MemVar. It moves the top of the stack to memory, leaving the result in ST(0). What does FSTP MemVar do? It pops the top of the stack to memory, moving ST(i) “up” to ST(i-1) for i = 1…7. What does this code do? .data varX REAL10 2.5 varY REAL10 -1.8 varZ REAL10 0.9 result REAL10 ? .code FINIT FLD varX FLD varY FLD varZ FMUL FADD FSTP result It initializes the FPU, then creates a postfix expression. result = (varY * varZ) + varX. How would you calculate the dot product of (6.0, 4.5) and (2.0, 3.2) using this array? .data finit fld array ;push 6.0 fld array+10 ;push 2.0 fmul ;ST(0) = 12.0 fld array + 20; push 4.5 fld array + 30; push 3.2 fmul ;ST(0) = 14.4 fadd ;ST(0) = ST(0) + ST(1) fstp dotProduct ;pop into variable Which structure is the following true for? Macros AND/OR/NOR Procedures ______________ have a calling mechanism involving the EIP. Procedures. MACROS are simply placed in line by line, so they don’t need to store any return address. Which structure is the following true for? Macros AND/OR/NOR Procedures _________ are translated only once, and can be called many times. Procedures. Macros are not called at all, since they are simply replacing wherever their nickname is. Suppose that a program’s data and executable code require 1024 bytes of memory. A new section of code must be added; it will be used with various values 74 times during the execution of a program. When implemented as a macro, the macro code requires 69 bytes of memory. When implemented as a procedure, the procedure code requires 154 bytes (including parameter-passing, etc.), and each procedure call requires 10 bytes. How many bytes of memory will the entire program require if the new code is added as a procedure? If added as a procedure, you just have to multiply how many times it is called by the procedure call (10 * 74) and add that to the bytes the code itself takes (154). 1024+740+154 = 1918. ppose that a program’s data and executable code require 1024 bytes of memory. A new section of code must be added; it will be used with various values 62 times during the execution of a program. When implemented as a macro, the macro code requires 105 bytes of memory. When implemented as a procedure, the procedure code requires 151 bytes (including parameter-passing, etc.), and each procedure call requires 11 bytes. How many bytes of memory will the entire program require if the new code is added as a macro? When added as a macro, you just multiply the storage of the macro (105) times how many times it is used(62). 1024 + 105*62 = 7534. What is the first number printed to the screen after this code executes? main PROC push 4 push 5 call rcrsn exit main ENDP rcrsn PROC push ebp mov ebp,esp mov eax,[ebp + 12] mov ebx,[ebp + 8] cmp eax,ebx jl recurse jmp quit recurse: inc eax push eax push ebx call rcrsn mov eax,[ebp + 12] call WriteDec Space 2 quit: pop ebp ret 8 rcrsn ENDP What is a good macro to write a string? How do you use it in the code? mWriteStr MACRO buffer push edx mov edx, OFFSET buffer call WriteString pop edx ENDM .code What is a good macro to read a string? How do you use it in code? mReadStr MACRO varName push ecx push edx mov edx, OFFSET variable mov ecx, (SIZEOF variable) call ReadString pop edx pop ecx ENDM .code Write a MASM macro that calculates x^2 – 1 for its parameter x, and stores the result in memory at the second parameter. The caller passes x by value, and the result variable by address. Quadratic MASM x, y Push eax Push ebx Push edi ;store the memory address Mov eax, x Mov ebx, x Mul eax, ebx Dec eax ;minus 1 Mov edi, addr ;save eax in memory address (edi) Mov [edi], eax Pop edi Pop ebx Pop eax ENDM How do you deal with labels ;in macro? You specify that a label is local!! Otherwise, it’s going to jump to wherever you’ve used the MACRO. seq macro a, b LOCAL test LOCAL quit mov eax, a mov ebx, b test: cmp eax, ebx ;if a What types of arguments can be used in macros? Any types!! There is no checking for memory, registers, or literals!! As long as it can be used in the statements of the macro, it’s fine. What is a recursive summation procedure? summation PROC push ebp mov ebp, esp mov eax, [ebp+16] ;eax = x mov ebx, [ebp+12] ; ebx = y add [edx], eax cmp eax, ebx je quit recurse: inc eax push eax push ebx push edx call summation quit: pop ebp ret 12 summation ENDP One goal of multiple internal buses is to simplify what process? Suppose that you are working with a CISC machine using a 1.6 GHz clock (i.e., the clock ticks 1.6 billion times per second). This particular computer uses MASM-like instructions with the following timings: add reg, mem 5 clock cycles (i.e., the ADD micro-program has 5 instructions) Also suppose you’re summing an array: After initialization, how many array elements can be processed in 3.6 ms? Round your answer to the nearest integer. Note that 1 ms. = 0.001 second. Since the clock ticks 1.6 billion times per second, we know that it ticks 1.6 million times per millisecond. Therefore, we also know that it ticks 5.76 million times per 3.6 ms. add bx, esi - 5 (5+4+8) = 17. Therefore, 5.76m/17 = 338, 824. True or false? Multiprocessor Parallelism (shared memory) usually has lower coordination overhead than Multicomputer Parallelism (distributed memory). Which of the following portions of a program can complicate the instruction-caching process? (Check all that apply) Sequential Execution
True or false? Software parallelism is currently much more developed than hardware parallelism. Assume you are working with the five-stage pipeline. Suppose that each stage requires 3.8 nanoseconds to complete its task. How many nanoseconds would it take to complete 63 instructions without pipelining? Round your answer to the nearest integer. It would take 63*5 *3.8 = 1197 nanoseconds. An algorithm takes 6.8 seconds to execute on a single 3.4 GHz processor. 58% of the algorithm is sequential. Assume that there is zero latency and that the remaining code exhibits perfect parallelism. How long (in seconds) should the algorithm take to execute on a parallel machine made of 4 3.4 GHz processors? Round answers to one decimal place. 6.8(.58) * (1-.58)*6.8/4 = 4.658 seconds. Assume that you are working with the five-stage pipeline shown in the diagram. Suppose that each stage requires 2.8 nanoseconds to complete its task. How many nanoseconds will it take to complete 77 instructions with pipelining? Since the first four instructions are blank, and the rest finish in that order, it’ll take (n+4) steps to finish n instructions. Therefore, (77+4)*2.8 = 227 nanoseconds. |