COMSC 260 Test 2
Suppose a program is supposed to swap adjacent pairs in an array that always has an even number of elements. For example the array [1, 2, 3, 4, 5, 6, 7, 8] would become [2, 1, 4, 3, 6, 5, 8, 7] The start of the program is as follows: .data array WORD 1,2,3,4,5,6,7,8,9,10 .code main PROC mov esi,0 mov ecx, LENGTHOF array / 2 Which of the options below is the correct continuation of the program?
L1: mov ax, array[esi] xchg ax, array[esi+2] mov array[esi], ax add esi, 4 loop L1
Which of the following options below is the correct way to end a string with the null terminator character?
Text BYTE "Hello World", 0
Which of the following options below represents a 16 bit unsigned integer?
WORD
Suppose a program has the following data segment: arrB BYTE 1, 1, 1, 1, 1, 1 And then the following instructions are executed in the code segment: mov al, [arrB+1] add al, arrB mov [arrB+2], al mov al, [arrB+2] add al, [arrB+1] mov [arrB+3], al What does arrB look like after these instructions have executed?
[1, 1, 2, 3, 1, 1]
Which of the following options below is the correct way to do a line return (new line) in x86 assembly language?
line_return BYTE 0Dh, 0Ah
Suppose a program has the following data segment: .data arr BYTE 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 And the goal is to change second value in the array to 99. After the program has finished arr should be: [1, 99, 3, 4, 5, 6, 7, 8, 9, 10] Which of the options below is the correct implementation of the program?
mov [arr+SIZEOF arr - (SIZEOF arr -1)], 99
Which of the following options below will 1) Produce the largest sum (and store it in al) AND 2) NOT produce a carry out of the MSB (most significant bit)? HINT: How many bits are registers al and bl?
mov al, 254 mov bl, 1 add al, bl
Which of the following options below will produce a carry out of the MSB (most significant bit)? HINT: How many bits are registers al and bl?
mov al, 255 mov bl, 1 add al, bl
Suppose the following is declared in the data segment: .data val1 WORD 2h, 1h All of the options below will move the number 2 into al EXCEPT:
mov al, SIZEOF val1
Suppose a program has the following array in the data segment: mov a arr BYTE 1, 2, 3, 4 And the goal is to remove the first and third elements in the array, and move the second and fourth elements up to the beginning of the array. Here an element will be "removed" by putting a 0 in it's place. After the program has finished running arr should be: [2, 4, 0, 0] Which of the options below is the correct implementation of the program?
mov al, [arr+LENGTHOF arr-3] mov [arr], al mov al, [arr+LENGTHOF arr-1] mov [arr+1], al mov [arr+LENGTHOF arr-2], 0 mov [arr+LENGTHOF arr-1], 0
Suppose a program has the following data segment: .data valA BYTE 1h valB BYTE 2h, 3h, 4h, 5h Which of the following mov instructions will store 4h in register al?
mov al, [valA+3]
Suppose the following array is declared in the data segment: .data arr BYTE 1, 2, 3, 4 And the goal of the program is to swap the first half and second half of the array. After the program is finished, arr should be: [3, 4, 1, 2] Which of the options below is the correct implementation of the program?
mov al, arr mov bl, [arr+LENGTHOF arr/2] mov arr, bl mov [arr+LENGTHOF arr/2], al mov al, [arr+1] mov bl, [arr+LENGTHOF arr/2+1] mov [arr+1], bl mov [arr+LENGTHOF arr/2+1], al
Suppose a program has the following array declared in the data segment: array WORD 1, 5, 7 The program is supposed to calculate the sum of the gaps in the array. A gap in an array is the distance between two adjacent elements in the array. For the array given above, the gaps are: 5 - 1 = 4 7 - 5 = 2 So the sum of the gaps is 4+2=6 Which of the options below is the correct implementation of the program?
mov ax, 0 mov bx, [array+2] sub bx, array add ax, bx mov bx, [array+4] sub bx, [array+2] add ax, bx
Which of the options below meets BOTH of the following criteria: The loop is a pretest loop. This means the condition for the loop is checked BEFORE executing the body of the loop. The body of the loop (i.e., add eax, eax) is executed exactly five times.
mov eax, 0 mov ecx, 6 jmp L2 L1: add eax, eax L2: loop L1
Suppose a program has the following data segment: arrB BYTE 01h, 23h, 45h, 67h, 89, 0ABh, 0CDh, 0EFh Which of the following instructions below will move AB896745h into eax?
mov eax, DWORD PTR [arrB+2]
Suppose a program has the following data segment: source BYTE "ASSEMBLY", 0 target BYTE LENGTHOF source dup('#') Which of the options below will move the address of the "L" into BOTH eax AND ebx?
mov eax, OFFSET source + SIZEOF source -3 mov ebx, OFFSET target - 3
Suppose a program is supposed to reverse an array. Part of the program is as follows: .data array DWORD 1,5,6,8,0Ah,1Bh,1Eh,22h,2Ah,32h .code mov edx, 0 L1: mov eax, array[edx] xchg eax, array[ebx] mov array[edx], eax add edx, TYPE array sub ebx, TYPE array loop L1 For this program to work correctly, ebx and ecx must both be initialized correctly. Which of the options below correctly initializes both registers?
mov ebx, SIZEOF array - TYPE array mov ecx, LENGTHOF array / 2
All of the options below will cause an infinite loop* EXCEPT: Here "infinite loop" means either a loop that literally never ends, or a loop that does eventually end but has many, many..... many more iterations than it normally should have (e.g., billions of loop iterations).
mov ecx, 0 L1: mov eax, ecx inc ecx loop L1
The relative offset used for jumping in a loop instruction is encoded by a single byte. What is the largest possible backward jump? What is the largest possible forward jump?
1. -128 2. +127
What is the logic behind the loop instruction?
1. ECX = ECX - 1 2. if ECX != 0, jump to target
1. LENGTHOF is equivalent to SIZEOF * TYPE 2. LENGTHOF returns the number of bytes in a data declaration.
1. False 2. False
Which of the options below will: 1) store 6 in eax 2) store 3 in ebx
.data arr WORD 99, 51, 73 WORD 23, 47, 61 WORD 17, 89, 93 .code mov eax, SIZEOF arr mov ebx, LENGTHOF arr
.data val1 BYTE ? val2 WORD ? val3 WORD ? val4 WORD ? Assuming the data segment starts at 0000 0000: mov esi, OFFSET val4 Which address would the above instruction move into esi?
0000 0005
X WORD 1A2Bh, 3C4Dh, 5E6Fh How is X stored in memory?
0000: 2B 0001: 1A 0002: 4D 0003: 3C 0004: 6F 0005: 5E
var1 BYTE 7 dup("COMSC 260") How many bytes is var1?
63
When a loop instruction is encountered, and the jump is taken, which register is affected by the jump?
EIP
Suppose a program is supposed to write to memory letter/digit pairs, where the letter comes first and the digit comes second. Each pairing of a capital letter (A - Z) and a digit (0 - 9) should be written to memory in the following order: A0, A1, ...., A9, B0, B1, .... B9, ...., Y0, Y1, .... Y9, Z0, Z1, .... Z9* In addition, the total number of pairings should be stored in num_of_pairs. *Remember if you were to actually run this in visual studio, you would see the ASCII codes in memory itself, and the characters ('A', '0', etc.) off to the side The start of this program is as follows: LETTERS = 26 DIGITS = 10 .data outer_loop DWORD ? pair WORD ? num_of_pairs DWORD ? .code main proc mov outer_loop, LETTERS mov ah, '0' mov al, 'A' mov pair, ax mov num_of_pairs, 1 Which of the options below is the correct continuation of this program? NOTE: inc is the increment instruction -- it increments the value of it's operand by one.
mov ecx, DIGITS-1 jmp L2 L1: mov outer_loop, ecx mov ecx, DIGITS-1 mov ah, '0' mov al, BYTE PTR pair inc al mov pair, ax inc num_of_pairs L2: inc BYTE PTR [pair+1] inc num_of_pairs loop L2 mov ecx, outer_loop loop L1
Suppose a program is supposed to calculate the sum of the gaps in an array. For example, for the array [0, 2, 5, 9, 10], the sum of the gaps is 2+3+4+1 = 10. The start of this program is as follows: .data dwarray dword 0,2,5,9,10 count = LENGTHOF dwarray result dword ? .code mov ebx, 0 mov edx, 0 Which of the options below will correctly continue the program and store the sum of the gaps in the array in the result variable?
mov ecx, count - 1 L1: mov eax, dwarray[ebx+4] sub eax, dwarray[ebx] add edx, eax add ebx, TYPE dwarray loop L1 mov result, edx
Suppose a program has the string "COMSC 260" stored in source. The goal of the program is to store the reversed string, "062 CSMOC" in target. Part of the program is as follows: .data source BYTE "COMSC 260", 0 target BYTE SIZEOF source DUP(?) .code main PROC mov ecx,SIZEOF source-1 L1: mov al, [esi] mov [edi], al dec esi inc edi loop L1 mov BYTE PTR [edi], 0 For this program to work correctly, esi and edi must both be initialized correctly. Which of the options below correctly initializes both registers? NOTE: The source string ends with a null terminator (0). After the program finishes running, the target string will also end with a null terminator (0). NOTE: inc is the increment instruction -- it increments the value of it's operand by one. Likewise dec is the decrement instruction -- it decrements the value of it's operand by one.
mov esi, OFFSET [target - 2] mov edi, OFFSET [source + SIZEOF source]
Suppose a program has the following memory layout: Relative Address Value 0000 'A' (ASCII code 41h) 0001 'B' (ASCII code 42h) 0002 'C' (ASCII code 43h) 0003 'D' (ASCII code 44h) 0004 'E' (ASCII code 45h) 0005 'F' (ASCII code 46h) 0006 'G' (ASCII code 47h) 0007 'H' (ASCII code 48h)
someval WORD 4241h, 4443h someval2 DWORD 45464748h
