Midterm Study Guide For Assembly Language
Midterm Study Guide For Assembly Language
5 General Concepts
1. The central processor unit (CPU) contains registers and what other basic elements?
Control Unit, Arithmetic Logic Unit, and the clock.
2. The central processor unit is connected to the rest of the computer system using what three
buses?
Data, Address, and Control buses.
3. Why does memory access take more machine cycles than register access?
Conventional memory is outside the CPU and it responds more slowly to access requests.
Registers are hard-wired inside the CPU.
4. What are the three basic steps in the instruction execution cycle?
Fetch, decode, execute.
5. Which two additional steps are required in the instruction execution cycle when a memory
operand is used?
Fetch memory operands, store memory operands.
6. During which stage of the instruction execution cycle is the program counter incremented?
During the fetch step.
7. When a program runs, what information does the OS read from the filenames disk directory
entry?
Section 2.1.4 mentions the filename, file size, and starting location on the disk. (Most
directories also store the files last modification date and time.)
8. After a program has been loaded into memory, how does it begin execution?
The OS executes a branch (like a GOTO) to the first machine instruction in the program.
9. Define multitasking
The CPU executes multiple tasks (programs) by rapidly switching from one program to the
next. This gives the impression that all programs are executing at the same time.
10. What is the function of the OS scheduler?
The OS scheduler determines how much time to allot to each task, and it switches between
tasks.
11. When the processor switches from one task to another, what values in the first tasks state
must be preserved?
The program counter, the tasks variables, and the CPU registers (including the status flags).
12. What is the duration of a single clock cycle in a 3-GHz processor?
3.33_10_10, which is 1.0/3.0_10_9.
2.2.5 x86 Architecture Details
1. What are the x86 processors three basic modes of operation?
Real-address mode, Protected mode, and System Management mode.
2. Name all eight 32-bit general-purpose registers.
EAX, EBX, ECX, EDX, ESI, EDI, ESP, EBP.
3. Name all six segment registers.
CS, DS, SS, ES, FS, GS.
4. What special purpose does the ECX register serve?
Loop counter.
5. Besides the stack pointer (ESP), what other register points to variables on the stack?
EBP.
6. Name at least four CPU status flags.
Most common: Carry, Sign, Zero, Overflow. Less common: Auxiliary Carry, Parity.
7. Which flag is set when the result of an unsigned arithmetic operation is too large to fit into the
destination?
Carry
8. Which flag is set when the result of a signed arithmetic operation is either too large or too
small to fit into the destination?
Overflow
9. Which flag is set when an arithmetic or logical operation generates a negative result?
Sign
10. Which part of the CPU performs floating-point arithmetic?
Floating-Point Unit
11. How many bits long are the FPU data registers?
80 Bits
12. Which Intel processor was the first member of the IA-32 family?
The Intel 80386.
13. Which Intel processor first introduced superscalar execution?
The Pentium.
14. Which Intel processor first used MMX technology?
The Pentium II.
15. Describe the CISC design approach.
CISC means complex instruction set: a large collection of instructions, some of which perform
sophisticated operations that might be typical of a high-level language.
16. Describe the RISC design approach.
The term RISC stands for reduced instruction set: a small set of simple (atomic) instructions
that may be combined into more complex operations.
2.3.3 x86 Memory Management
1. What is the range of addressable memory in protected mode?
4 GByte ( 0 to FFFFFFFFh).
2. What is the range of addressable memory in real-address mode?
1 MByte (0 to FFFFFh).
3. The two ways of describing an address in real-address mode are segment-offset and
Linear (absolute).
4. In real-address mode, convert the following hexadecimal segment-offset address to a linear
address: 0950:0100.
09600h
5. In real-address mode, convert the following hexadecimal segment-offset address to a linear
address: 0CD1:02E0.
0CFF0h.
6. In MASMs flat segmentation model, how many bits hold the address of an instruction or
variable?
32 bits.
7. In protected mode, which register references the descriptor for the stack segment?
SS register.
8. In protected mode, which table contains pointers to memory segments used by a single
program?
Local descriptor table.
9. In the flat segmentation model, which table contains pointers to at least two segments?
Global descriptor table.
10. What is the main advantage to using the paging feature of x86 processors?
The total size of all programs loaded into memory can exceed the amount of physical
memory installed in the computer.
11. Challenge: Can you think of a reason why MS-DOS was not designed to support protectedmode
programming?
This is an open-ended question, of course. It is a fact that MS-DOS first had to run on the
8086/8088 processors, which only supported Real-address mode. When later processors
came out that supported Protected mode, my guess is that Microsoft wanted MS-DOS to
continue to run on the older processors. Otherwise, customers with older computers would
refuse to upgrade to new versions of MS-DOS.
12. Challenge: In real-address mode,
The following segment-offset addresses point to the same linear address: 0640:0100 and
0630:0200.
2. Declare a symbolic constant named SecondsInDay using the equal-sign directive and
assign it an arithmetic expression that calculates the number of seconds in a 24-hour period.
SecondsInDay =24 * 60 * 60
3. Write a statement that causes the assembler to calculate the number of bytes in the following
array, and assign the value to a symbolic constant named ArraySize:
myArray WORD 20 DUP(?)
ArraySize= ( $ - myArray)
4. Show how to calculate the number of elements in the following array, and assign the value
to a symbolic constant named ArraySize:
myArray DWORD 30 DUP(?)
7. Use TEXTEQU to assign the symbol SetupESI to the following line of code:
mov esi,OFFSET myArray
7. For each of the following statements, state whether or not the instruction is valid:
a.
b.
c.
d.
e.
f.
g.
h.
mov ax,var1
mov ax,var2
mov eax,var3
mov var2,var3
movzx ax,var2
movzx var2,al
mov ds,ax
mov ds,1000h
(a) not valid (b) valid (c) not valid (d) not valid (e) not valid (f) not valid (g) valid (h) not
valid
8. What will be the hexadecimal value of the destination operand after each of the following
instructions execute in sequence?
mov al,var1
mov ah,[var1+3]
; a.
; b.
9. What will be the value of the destination operand after each of the following instructions
execute in sequence?
mov
mov
mov
mov
ax,var2
ax,[var2+4]
ax,var3
ax,[var3-2]
;
;
;
;
a.
b.
c.
d.
;
;
;
;
a.
b.
c.
d.
4. If val2 is incremented by 1 using the ADD instruction, what will be the values of the Carry
and Sign flags?
CF=0, SF=1.
5. If val4 is incremented by 1 using the ADD instruction, what will be the values of the Overflow
and Sign flags?
OF= 1, SF=1.
6. Where indicated, write down the values of the Carry, Sign, Zero, and Overflow flags after
each instruction has executed:
mov ax,7FF0h
add al,10h ;
a. CF = SF = ZF = OF =
add ah,1 ;
b. CF = SF = ZF = OF =
add ax,2 ;
c. CF = SF = ZF = OF =
(a) CF=1, SF=0, ZF=1, OF=0
(b) CF=0, SF=1, ZF=0, OF=1
(c) CF=0, SF=1, ZF=0, OF=0
7. Implement the following expression in assembly language: AX _ (_val2 _ BX) _ val4.
mov
neg
add
sub
ax,val2
ax
ax,bx
ax,val4
8. (Yes/No): Is it possible to set the Overflow flag if you add a positive integer to a negative integer?
No
9. (Yes/No): Will the Overflow flag be set if you add a negative integer to a negative integer
and produce a positive result?
Yes
10. (Yes/No): Is it possible for the NEG instruction to set the Overflow flag?
Yes (for example, mov al, -128 . . . followed by . . . neg al).
11. (Yes/No): Is it possible for both the Sign and Zero flags to be set at the same time?
No
12. Write a sequence of two instructions that set both the Carry and Overflow flags at the same
time.
mov al,80h
add al,80h
13. Write a sequence of instructions showing how the Zero flag could be used to indicate unsigned
overflow after executing INC and DEC instructions.
mov al,0FFh
inc al
jz overflow_occurred
mov bl,1
dec bl
jz overflow_occurred
6. Insert a directive in the given data that aligns myBytes to an even-numbered address.
.data
ALIGN 2
myBytes BYTE 10h, 20h, 30h, 40h
etc.
7. What will be the value of EAX after each of the following instructions execute?
mov
mov
mov
mov
mov
mov
mov
eax,TYPE myBytes
eax,LENGTHOF myBytes
eax,SIZEOF myBytes
eax,TYPE myWords
eax,LENGTHOF myWords
eax,SIZEOF myWords
eax,SIZEOF myString
;
;
;
;
;
;
;
a.
b.
c.
d.
e.
f.
g.
12. Insert a LABEL directive in the given data that permits myBytes to be moved directly to a
16-bit register.
myBytesW LABEL WORD
myBytes BYTE 10h,20h,30h,40h
.code
mov ax,myBytesW
7. Fill in the requested register values on the right side of the following instruction sequence:
mov
mov
mov
mov
mov
mov
mov
mov
mov
mov
esi,OFFSET myBytes
al,[esi]
al,[esi+3]
esi,OFFSET myWords + 2
ax,[esi]
edi,8
edx,[myDoubles + edi]
edx,myDoubles[edi]
ebx,myPointer
eax,[ebx+4]
; a. AL =
; b. AL =
; c. AX =
; d. EDX =
; e. EDX =
; f. EAX =
esi,OFFSET myBytes
ax,[esi]
eax,DWORD PTR myWords
esi,myPointer
ax,[esi+2]
ax,[esi+6]
ax,[esi-4]
; a. AX =
; b. EAX =
; c. AX =
; d. AX =
; e. AX =
5. (True/False): The LOOP instruction does the following: It decrements ECX; then, if ECX is
not equal to zero, LOOP jumps to the destination label.
True
6. In real-address mode, which register is used as the counter by the LOOP instruction?
CX
7. In real-address mode, which register is used as the counter by the LOOPD instruction?
ECX
8. (True/False): The target of a LOOP instruction must be within 256 bytes of the current location.
False (-128 to +127 bytes from the current location).
9. (Challenge): What will be the final value of EAX in this example?
mov eax,0
mov ecx,10 ; outer loop counter
L1:
mov eax,3
mov ecx,5 ; inner loop counter
L2:
add eax,5
loop L2 ; repeat inner loop
loop L1 ; repeat outer loop
This is a trick! The program does not stop, because the first LOOP instruction decrements ECX to
zero. The second LOOP instruction decrements ECX to FFFFFFFFh, causing the outer loop
to repeat.
10. Revise the code from the preceding
Insert the following instruction at label L1: push ecx, Also insert the following instruction before the
second LOOP instruction: pop ecx. (Once you have added these instructions,the final value of eax is
1Ch.)
5.2.2 Linking to an External Library
1.(True/False):A link library consists of assembly language source code.
False (it contains object code).
2. Use the PROTO directive to declare a procedure named MyProc in an external link library.
MyProc PROTO
3. Write a CALL statement that calls a procedure named MyProc in an external link library.
call MyProc
4. What is the name of the 32-bit link library supplied with this book?
Irvine32.lib.
5. Which library contains functions called from Irvine32.lib?
Kernel32.lib.
6. What type of file is kernel32.dll?
Kernel32.dll is a dynamic link library that is a fundamental part of the MS-Windows
operating system.
5.3.4 The Books Link Library
1. Which procedure in the link library generates a random integer within a selected range?
RandomRange procedure.
2. Which procedure in the link library displays Press [Enter] to continue. . . and waits for the
user to press the Enter key?
WaitMsg procedure.
3. Write statements that cause a program to pause for 700 milliseconds.
mov eax,700
call Delay
4. Which procedure from the link library writes an unsigned integer to the console window in
decimal format?
WriteDec procedure.
5. Which procedure from the link library places the cursor at a specific console window
location?
Gotoxy procedure.
6. Write the INCLUDE directive that is required when using the Irvine32 library.
INCLUDE Irvine32.inc.
7. What types of statements are inside the Irvine32.inc file?
PROTO statements (procedure prototypes) and constant definitions. (There are also text
macros, but they are not mentioned in this chapter.)
8. What are the required input parameters for the DumpMem procedure?
ESI contains the datas starting address, ECX contains the number of data units, and EBX
contains the data unit size (byte, word, or doubleword).
9. What are the required input parameters for the ReadString procedure?
EDX contains the offset of an array of bytes, and ECX contains the maximum number of
characters to read.
10. Which processor status flags are displayed by the DumpRegs procedure?
Carry, Sign, Zero, Overflow, Auxiliary carry, and Parity.
11. Challenge: Write statements that prompt the user for an identification number and input a
string of digits into an array of bytes.
.data
str1 BYTE "Enter identification number: ",0
idStr BYTE 15 DUP(?)
.code
mov edx,OFFSET str1
call WriteString
mov edx,OFFSET idStr
mov ecx,(SIZEOF idStr) - 1
call ReadString
13. Challenge: Suppose there were no PUSH instruction. Write a sequence of two other instructions
that would accomplish the same as PUSH EAX.
sub esp,4
mov [esp],eax
A stub program contains all of its important procedures, but the procedures are
either
empty or nearly empty.
4. (True/False): The ArraySum procedure of the Summation program (Section 5.6.1)
directly
references the name of an array variable.
False (it receives a pointer to an array).
5. Which lines in the PromptForIntegers procedure of the Summation program (Section
5.6.1)
would have to be modified so it could handle an array of 16-bit words? Create such a
version
and test it.
The following statements would have to be modified:
mov [esi],eax becomes --> mov [esi],ax
add esi,4 becomes --> add esi,2
; a.
; b.
; c.
; d.
; a.
; b.
; c.
; d.
5. Write a single instruction using 16-bit operands that sets the high 8 bits of AX and does not
change the low 8 bits
.or ax,0FF00h
6. Write a single instruction (other than NOT) that reverses all the bits in EAX.
xor eax,0FFFFFFFFh
7. Write instructions that set the Zero flag if the 32-bit value in EAX is even and clear the Zero
flag if EAX is odd.
test eax,1 ; (low bit set if eax is odd)
8. Write a single instruction that converts an uppercase character in AL to lowercase but does
not modify AL if it already contains a lowercase letter.
or al,00100000b
9. Write a single instruction that converts an ASCII digit in AL to its corresponding binary value. If AL already
contains a binary value (00h to 09h), leave it unchanged.
and al,00001111b
10. Write instructions that calculate the parity of the 32-bit memory operand. Hint: Use the
formula presented earlier in this section: B 0 XOR B1 XOR B2 XOR B3.
.data
memVal DWORD ?
.code
mov al,BYTE PTR
xor al,BYTE PTR
xor al,BYTE PTR
xor al,BYTE PTR
memVal
memVal+1
memVal+2
memVal+3
11. Given two bit-mapped sets named SetX and SetY, write a sequence of instructions that generate
a bit string in EAX that represents members in SetX that are not members of SetY.
; Method 1: X - (X intersection Y)
mov eax,SetX ; X
mov edx,eax ; (X intersection Y)
and edx,SetY
sub eax,edx ; X - (X intersection Y)
; Method 2: (X union Y) - Y
mov eax,SetX
or eax,SetY ; X union Y
sub eax,SetY ; (X union Y) Y
10. (Yes/No): Will the following code jump to the label named Target?
mov ax,-42
cmp ax,26
ja Target
12. Write instructions that jump to label L2 when the signed integer in AX is greater than the
integer in CX.
cmp ax,cx
jg L2
13. Write instructions that first clear bits 0 and 1 in AL. Then, if the destination operand is equal
to zero, the code should jump to label L3. Otherwise, it should jump to label L4.
and al,11111100b
jz L3
jmp L4
5. Challenge: The LOOPNZ example in Section 6.4.2 relies on a sentinel value to handle the possibility
that a positive value might not be found. What might happen if we removed the sentinel?
If a matching value were not found, ESI would end up pointing beyond the end of the
array. By pointing at an undefined memory location, a program runs the risk of causing a
runtime error.
6.5.5 Conditional Structures
Notes: In all compound expressions, use short-circuit evaluation. Assume that val1 and X are 32-bit
variables.
6. In the program from Section 6.5.4, why is it better to let the assembler calculate
NumberOfEntries rather than assigning a constant such as NumberOfEnteries _ 4?
Future changes to the table will alter the value of NumberOfEntries. We might forget to
update the constant manually, but the assembler can correctly adjust a calculated value.
7. Challenge: Rewrite the code from Section 6.5.3 so it is functionally equivalent, but uses
fewer instructions.
.data
sum DWORD 0
sample DWORD 50
array DWORD 10,60,20,33,72,89,45,65,72,18
ArraySize = ($ - Array) / TYPE array
.code
mov eax,0 ; sum
mov edx,sample
mov esi,0 ; index
mov ecx,ArraySize
L1: cmp esi,ecx
jnl L5
cmp array[esi*4],edx
jng L4
add eax,array[esi*4]
L4: inc esi
jmp L1
L5: mov sum,eax