0% found this document useful (0 votes)
70 views24 pages

Assembly Section Reviews

Uploaded by

Sheikh Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views24 pages

Assembly Section Reviews

Uploaded by

Sheikh Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.1.

3 Section Review
1. How do assemblers and linkers work together?

 Assembler converts assembly code to machine code.


 Linker combines machine code files into a single executable.

2. How will studying assembly language enhance your understanding of


operating systems?

 It helps you understand how the OS interacts with hardware at a low level.

3. What is meant by a one-to-many relationship when comparing a high-level


language to machine language?

 A single high-level instruction can translate to multiple machine instructions.

4. Explain the concept of portability as it applies to programming languages.

 Portability means a program can run on different systems with little change.

5. Is the assembly language for x86 processors the same as those for computer
systems such as the VAX or Motorola 68x00?

 No, assembly language differs by processor architecture.

6. Give an example of an embedded systems application.

 A smart thermostat.

7. What is a device driver?

 Software that allows the OS to communicate with hardware.

8. Do you suppose type checking on pointer variables is stronger (stricter) in


assembly language, or in C and C++?

 It is stricter in C and C++.

9. Name two types of applications that would be better suited to assembly


language than a high-level language.

 Real-time systems and performance-critical applications.


10. Why would a high-level language not be an ideal tool for writing a program
that directly accesses a printer port?

 High-level languages abstract hardware access; assembly allows direct interaction.

11. Why is assembly language not usually used when writing large application
programs?

 It's harder to write, maintain, and less portable.

12. Challenge: Translate the following C++ expression to assembly language:


makefile
Copy code
X = (Y * 4) + 3;

Assembly (x86):

sql
Copy code
MOV EAX, [Y] ; Load Y
IMUL EAX, 4 ; Multiply by 4
ADD EAX, 3 ; Add 3
MOV [X], EAX ; Store result in X

1.2.1 Section Review


1. What is the virtual machine concept?
Answer: A virtual machine is a software simulation of a physical computer, allowing
programs to run as if on actual hardware.
2. Why do you suppose translated programs often execute more quickly than interpreted
ones?
Answer: Translated programs are pre-converted into machine code, which can be directly
executed by the CPU, while interpreted programs need to be translated line by line during
execution.
3. (True/False): When an interpreted program written in language L1 runs, each of its
instructions is decoded and executed by a program written in language L0.
Answer: True.
4. Explain the importance of translation when dealing with languages at different virtual
machine levels.
Answer: Translation ensures that a program written in one language can run on different
hardware or software systems by converting it into a form understood by the virtual machine.
5. At which level does assembly language appear in the virtual machine example shown in
this section?
Answer: Assembly language appears at a lower level, just above machine language in the
virtual machine hierarchy.
6. What software utility permits compiled Java programs to run on almost any computer?
Answer: The Java Virtual Machine (JVM) allows compiled Java programs to run on almost
any computer.
7. Name the four virtual machine levels named in this section, from lowest to highest.
Answer: Machine level, Assembly level, High-level language, and Application level.
8. Why don’t programmers write applications in machine language?
Answer: Machine language is complex and hard to write and debug, making it impractical
for most programmers.
9. Machine language is used at which level of the virtual machine shown in Figure 1-1?
Answer: Machine language is used at the lowest virtual machine level, directly interacting
with hardware.
10. Statements at the assembly language level of a virtual machine are translated into
statements at which other level?
Answer: Assembly language is translated into machine language.

Chapter 2

Certainly! Here are the questions with the simplest answers:

1. Describe SRAM and its most common use.


SRAM: Static RAM, fast memory used for cache and temporary storage.
2. Describe VRAM.
VRAM: Video RAM, used for storing video data in graphics cards.
3. List at least two features found in the Intel P965 Express chipset.
Intel P965 Features: Supports DDR2/DDR3 memory, and includes PCIe 1.0 support.
4. Name four types of RAM mentioned in this chapter.
Types of RAM: DRAM, SRAM, DDR, and DDR2.
5. What is the purpose of the 8259A PIC controller?
8259A PIC: Manages interrupts in the system, prioritizing and handling interrupt
requests.

Here are the questions with the simplest answers:

1. Of the four levels of input/output in a computer system, which is the most universal
and portable?
Answer: The operating system level is the most universal and portable.
2. What characteristics distinguish BIOS-level input/output?
Answer: BIOS-level I/O is low-level, hardware-specific, and provides basic functions
like booting.
3. Why are device drivers necessary, given that the BIOS already has code that
communicates with the computer’s hardware?
Answer: Device drivers are necessary for advanced, OS-specific control of hardware,
beyond BIOS capabilities.
4. In the example regarding displaying a string of characters, which level exists
between the operating system and the video controller card?
Answer: The device driver level exists between the OS and the video controller card.
5. Is it likely that the BIOS for a computer running MS-Windows would be different
from that used by a computer running Linux?
Answer: No, the BIOS is hardware-specific, not OS-specific, so it would be the same for
both MS-Windows and Linux.

Here are the questions with the answers unbolded:

1. In 32-bit mode, aside from the stack pointer (ESP), what other register points to
variables on the stack?
Answer: The base pointer (EBP).
2. Name at least four CPU status flags.
Answer: Carry flag (CF), Zero flag (ZF), Sign flag (SF), Overflow flag (OF).
3. Which flag is set when the result of an unsigned arithmetic operation is too large to
fit into the destination?
Answer: Carry flag (CF).
4. Which flag is set when the result of a signed arithmetic operation is either too large
or too small to fit into the destination?
Answer: Overflow flag (OF).
5. (True/False): When a register operand size is 32 bits and the REX prefix is used, the
R8D register is available for programs to use.
Answer: True.
6. Which flag is set when an arithmetic or logical operation generates a negative
result?
Answer: Sign flag (SF).
7. Which part of the CPU performs floating-point arithmetic?
Answer: The Floating Point Unit (FPU).
8. On a 32-bit processor, how many bits are contained in each floating-point data
register?
Answer: 80 bits.
9. (True/False): The x86-64 instruction set is backward-compatible with the x86
instruction set.
Answer: True.
10. (True/False): In current 64-bit chip implementations, all 64 bits are used for
addressing.
Answer: False.
11. (True/False): The Itanium instruction set is completely different from the x86
instruction set.
Answer: True.
12. (True/False): Static RAM is usually less expensive than dynamic RAM.
Answer: False.
13. (True/False): The 64-bit RDI register is available when the REX prefix is used.
Answer: True.
14. (True/False): In native 64-bit mode, you can use 16-bit real mode, but not the
virtual-8086 mode.
Answer: False.
15. (True/False): The x86-64 processors have 4 more general-purpose registers than the
x86 processors.
Answer: True.
16. (True/False): The 64-bit version of Microsoft Windows does not support virtual-
8086 mode.
Answer: True.
17. (True/False): DRAM can only be erased using ultraviolet light.
Answer: False.
18. (True/False): In 64-bit mode, you can use up to eight floating-point registers.
Answer: True.
19. (True/False): A bus is a plastic cable that is attached to the motherboard at both
ends, but does not sit directly on the motherboard.
Answer: False.
20. (True/False): CMOS RAM is the same as static RAM, meaning that it holds its
value without any extra power or refresh cycles.
Answer: False.
21. (True/False): PCI connectors are used for graphics cards and sound cards.
Answer: True.
22. (True/False): The 8259A is a controller that handles external interrupts from
hardware devices.
Answer: True.
23. (True/False): The acronym PCI stands for programmable component interface.
Answer: False (It stands for Peripheral Component Interconnect).
24. (True/False): VRAM stands for virtual random access memory.
Answer: False (It stands for Video RAM).
25. At which level(s) can an assembly language program manipulate input/output?
Answer: BIOS, operating system, and hardware level.
26. Why do game programs often send their sound output directly to the sound card’s
hardware ports?
Answer: To reduce latency and improve performance by bypassing the operating system's
audio processing.

Chapter 3

Here are the questions with the simplest answers:

1. Using the value –35, write it as an integer literal in decimal, hexadecimal, octal, and
binary formats that are consistent with MASM syntax.
Answer:
 Decimal:

 -35 (this is simply the negative value in decimal).

 Hexadecimal:
The two's complement binary 11011101 in hexadecimal is DCh.

 Octal:
The two's complement binary 11011101 in octal is 335o.

 Binary:
The two's complement binary representation is 11111111 11111111 11111111 11011101 (32-
bit format in MASM)

2. (Yes/No): Is A5h a valid hexadecimal literal?


Answer: Yes
3. (Yes/No): Does the multiplication operator (*) have a higher precedence than the
division operator (/) in integer expressions?
Answer: No (Multiplication and division have the same precedence.)
4. Create a single integer expression that uses all the operators from Section 3.1.2.
Calculate the value of the expression.
Answer: Example expression: 5 + 3 * 2 - 6 / 2
Calculation:
o 5 + (3 * 2) - (6 / 2)
o 5+6-3=8
5. Write the real number –6.2 * 10^4 as a real number literal using MASM syntax.
Answer: -6.2e4
6. (Yes/No): Must string literals be enclosed in single quotes?
Answer: Yes
7. Reserved words can be instruction mnemonics, attributes, operators, predefined
symbols, and __________.
Answer: directives
8. What is the maximum length of an identifier?
Answer: 255 characters

Here are the answers for Section 3.2.4:

1. In the AddTwo program, what is the meaning of the INCLUDE directive?


Answer: The INCLUDE directive is used to insert the contents of an external file (such
as a library or header file) into the program, typically for macros, constants, or other
reusable code.
2. In the AddTwo program, what does the .CODE directive identify?
Answer: The .CODE directive identifies the section of the program that contains
executable instructions or the code segment.
3. What are the names of the two segments in the AddTwo program?
Answer: The two segments in the AddTwo program are .DATA (for data storage) and
.CODE (for executable instructions).
4. In the AddTwo program, which register holds the sum?
Answer: The AX register holds the sum in the AddTwo program.
5. In the AddTwo program, which statement halts the program?
Answer: The INT 20h statement halts the program, which is an interrupt to terminate the
program in MS-DOS.

Here are the answers for Section 3.3.3:

1. What types of files are produced by the assembler?


Answer: The assembler produces object files (typically with a .obj extension).
2. (True/False): The linker extracts assembled procedures from the link library and
inserts them in the executable program.
Answer: True
3. (True/False): When a program’s source code is modified, it must be assembled and
linked again before it can be executed with the changes.
Answer: True
4. Which operating system component reads and executes programs?
Answer: The operating system's loader reads and executes programs.
5. What types of files are produced by the linker?
Answer: The linker produces executable files (typically with a .exe or .out extension)
and dynamic link libraries (DLLs) or shared libraries (e.g., .dll, .so).

Here are the answers for Section 3.4.13:

1. Create an uninitialized data declaration for a 16-bit signed integer.


Answer:
2. myInteger SWORD ?
3. Create an uninitialized data declaration for an 8-bit unsigned integer.
Answer:
4. myByte BYTE ?
5. Create an uninitialized data declaration for an 8-bit signed integer.
Answer:
6. mySignedByte SBYTE ?
7. Create an uninitialized data declaration for a 64-bit integer.
Answer:
8. myLargeInteger QWORD ?
9. Which data type can hold a 32-bit signed integer?
Answer: DWORD (Double Word).

Here are the answers for Section 3.5.5:

1. Declare a symbolic constant using the equal-sign directive that contains the ASCII
code (08h) for the Backspace key.
Answer:
2. BACKSPACE EQU 08h
3. 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.
Answer:
4. SECONDSINDAY EQU 24 * 60 * 60 ; 24 hours * 60 minutes * 60 seconds
5. 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:
6. ArraySize EQU $ - myArray
7. Show how to calculate the number of elements in the following array, and assign the
value to a symbolic constant named ArraySize:
Answer:
8. ArraySize EQU $ / 4 ; Since each DWORD is 4 bytes
9. Use a TEXTEQU expression to redefine “proc” as “procedure.”
Answer:
10. proc TEXTEQU procedure
11. Use TEXTEQU to create a symbol named Sample for a string constant, and then
use the symbol when defining a string variable named MyString.
Answer:
12. Sample TEXTEQU "Hello, World!"
13. MyString DB Sample
14. Use TEXTEQU to assign the symbol SetupESI to the following line of code:
Answer:
15. SetupESI TEXTEQU mov esi,OFFSET myArray

Here are the answers for 3.9.1 Short Answer:

1. Provide examples of three different instruction mnemonics.


Answer:
o MOV
o ADD
o SUB
2. What is a calling convention, and how is it used in assembly language declarations?
Answer: A calling convention is a set of rules that define how functions receive
parameters and return values. It specifies how arguments are passed (e.g., via registers or
the stack) and how the return address is handled.
3. How do you reserve space for the stack in a program?
Answer: You reserve space for the stack using the STACK directive, like so:
4. .STACK 100h
5. Explain why the term assembler language is not quite correct.
Answer: The term "assembler language" is not entirely accurate because assembly
language refers to human-readable instructions specific to an architecture, while the
assembler is the tool that translates those instructions into machine code.
6. Explain the difference between big endian and little endian. Also, look up the origins
of this term on the Web.
Answer:
o Big endian stores the most significant byte at the lowest memory address.
o Little endian stores the least significant byte at the lowest memory address.
The terms originated from Jonathan Swift's "Gulliver's Travels" where two
factions argued over which end of an egg should be broken first.
7. Why might you use a symbolic constant rather than an integer literal in your code?
Answer: Symbolic constants make the code more readable, easier to maintain, and less
error-prone by allowing changes in one place instead of throughout the program.
8. How is a source file different from a listing file?
Answer: A source file contains the original code written by the programmer, while a
listing file contains the assembly language code along with additional information such
as line numbers and errors produced during the assembly process.
9. How are data labels and code labels different?
Answer: Data labels refer to memory locations that hold data (e.g., variables), while
code labels mark locations in the program where instructions or blocks of code are
located (e.g., loops or procedure calls).
10. (True/False): An identifier cannot begin with a numeric digit.
Answer: True
11. (True/False): A hexadecimal literal may be written as 0x3A.
Answer: True
12. (True/False): Assembly language directives execute at runtime.
Answer: False (Directives are instructions for the assembler, not for execution at
runtime.)
13. (True/False): Assembly language directives can be written in any combination of
uppercase and lowercase letters.
Answer: True (Assembly language is case-insensitive for directives.)
14. Name the four basic parts of an assembly language instruction.
Answer:
o Mnemonic (operation)
o Operand(s) (data or registers)
o Optional modifiers (for size, etc.)
o Comment (optional, for explanation)
15. (True/False): MOV is an example of an instruction mnemonic.
Answer: True
16. (True/False): A code label is followed by a colon (:), but a data label does not end
with a colon.
Answer: False (Both code labels and data labels end with a colon.)
17. Show an example of a block comment.
Answer:
18. ; This is a block comment
19. ; It can span multiple lines
20. Why is it not a good idea to use numeric addresses when writing instructions that
access variables?
Answer: Numeric addresses are hard to maintain and understand. Using symbolic names
for variables makes the code more readable and portable.
21. What type of argument must be passed to the ExitProcess procedure?
Answer: An integer argument (exit code).
22. Which directive ends a procedure?
Answer: ENDP (return instruction) typically ends a procedure.
23. In 32-bit mode, what is the purpose of the identifier in the END directive?
Answer: The identifier in the END directive marks the entry point of the program
(typically the starting address of the main procedure).
24. What is the purpose of the PROTO directive?
Answer: The PROTO directive is used to declare the prototype of a procedure,
specifying its name and the parameters it accepts.
25. (True/False): An Object file is produced by the Linker.
Answer: False (Object files are produced by the assembler, not the linker.)
26. (True/False): A Listing file is produced by the Assembler.
Answer: True
27. (True/False): A link library is added to a program just before producing an
Executable file.
Answer: True
28. Which data directive creates a 32-bit signed integer variable?
Answer: DWORD
29. Which data directive creates a 16-bit signed integer variable?
Answer: WORD
30. Which data directive creates a 64-bit unsigned integer variable?
Answer: QWORD
31. Which data directive creates an 8-bit signed integer variable?
Answer: BYTE
32. Which data directive creates a 10-byte packed BCD variable?
Answer: DB 10 DUP(?) (10 bytes can be used to store packed BCD values).

Unit 4

4.1.10 Section Review

1. Three basic types of operands:


o Register, Memory, Immediate
2. (True/False): The destination operand of a MOV instruction cannot be a segment
register.
o True.
3. (True/False): In a MOV instruction, the second operand is known as the destination
operand.
o True.
4. (True/False): The EIP register cannot be the destination operand of a MOV
instruction.
o True.
5. What does reg/mem32 indicate?
o A 32-bit register or memory.
6. What does imm16 indicate?
o A 16-bit immediate value.
4.2.8 Section Review Answers:

Given Data:
.data
val1 BYTE 10h
val2 WORD 8000h
val3 DWORD 0FFFFh
val4 WORD 7FFFh

1. Write an instruction that increments val2.


2. inc val2
3. Write an instruction that subtracts val3 from EAX.
4. sub eax, val3
5. Write instructions that subtract val4 from val2.
6. sub val2, val4
7. If val2 is incremented by 1 using the ADD instruction, what will be the values of the
Carry and Sign flags?
o Carry Flag (CF): No carry occurs, so CF = 0.
o Sign Flag (SF): The value of val2 (8000h) is a negative number in two's complement, so
after adding 1, it will become 8001h, which is still negative. Therefore, SF = 1.
8. If val4 is incremented by 1 using the ADD instruction, what will be the values of the
Overflow and Sign flags?
o Overflow Flag (OF): Adding 1 to 7FFFh will cause an overflow because it exceeds the
maximum value a WORD can hold (FFFFh). So, OF = 1.
o Sign Flag (SF): The result will be 8000h, which is negative in two's complement, so SF =
1.

For the flag values after each instruction:

Instructions:

mov ax,7FF0h
add al,10h
add ah,1
add ax,2

 Step 1: mov ax, 7FF0h


o The AX register is loaded with 7FF0h, so the flags are not affected by a MOV instruction.
o CF = 0, SF = 0, ZF = 0, OF = 0
 Step 2: add al, 10h
o AL = 0F0h + 10h = 100h (which sets AL to 00h and generates a carry out).
o CF = 1 (Carry), SF = 0 (No negative result), ZF = 1 (Zero result), OF = 0 (No overflow)
 Step 3: add ah, 1
o AH = 7Fh + 1 = 80h (which sets the AH to 80h, negative in two's complement).
o CF = 0 (No carry), SF = 1 (Negative result), ZF = 0 (Non-zero result), OF = 0 (No
overflow)
 Step 4: add ax, 2
o AX = 8000h + 2 = 8002h (no carry or overflow, but result is negative).
o CF = 0 (No carry), SF = 1 (Negative result), ZF = 0 (Non-zero result), OF = 0 (No
overflow)

Final Flag Values:

 a. After add al, 10h:


CF = 1, SF = 0, ZF = 1, OF = 0
 b. After add ah, 1:
CF = 0, SF = 1, ZF = 0, OF = 0
 c. After add ax, 2:
CF = 0, SF = 1, ZF = 0, OF = 0

4.3.8 Section Review

1. (True/False): The OFFSET operator always returns a 16-bit value.


False – OFFSET returns an address, and its size depends on the mode (16-bit or 32-bit).
2. (True/False): The PTR operator returns the 32-bit address of a variable.
False – PTR specifies operand size, not the address.
3. (True/False): The TYPE operator returns a value of 4 for doubleword operands.
True – TYPE returns 4 for a doubleword (DWORD).
4. (True/False): The LENGTHOF operator returns the number of bytes in an
operand.
True – LENGTHOF returns the total size of an array in bytes.
5. (True/False): The SIZEOF operator returns the number of bytes in an operand.
True – SIZEOF returns the size of an operand in bytes.

Section 4.4.5:

1. (True/False): Any 32-bit general-purpose register can be used as an indirect


operand.
True – Any 32-bit general-purpose register (like EAX, EBX, ECX, EDX, ESI, EDI, etc.) can
be used as an indirect operand.
2. (True/False): The EBX register is usually reserved for addressing the stack.
False – The EBX register is not typically reserved for stack addressing. It is often used for
general-purpose purposes. The ESP and EBP registers are usually used for stack
addressing.
3. (True/False): The following instruction is invalid: inc [esi]
False – The instruction inc [esi] is valid. It increments the value at the memory
location pointed to by ESI.
4. (True/False): The following is an indexed operand: array[esi]
True – array[esi] is an indexed operand, where ESI is used as an index to access the
array.
5. Question 5 & 6

Let's solve Question 5 and Question 6 using the provided data definitions and instructions.

Data Definitions:

myBytes: BYTE 10h, 20h, 30h, 40h

myWords: WORD 8Ah, 3Bh, 72h, 44h, 66h

myDoubles: DWORD 1, 2, 3, 4, 5

myPointer: DWORD pointing to myDoubles

---

Question 5:

mov esi, OFFSET myBytes


mov al, [esi] ; a. AL = ?
mov al, [esi+3] ; b. AL = ?
mov esi, OFFSET myWords + 2
mov ax, [esi] ; c. AX = ?
mov edi, 8
mov edx, [myDoubles + edi] ; d. EDX = ?
mov edx, myDoubles[edi] ; e. EDX = ?
mov ebx, myPointer
mov eax, [ebx+4] ; f. EAX = ?

Solution:

1. mov esi, OFFSET myBytes


ESI now points to the myBytes array.
myBytes = [10h, 20h, 30h, 40h]

2. mov al, [esi]


AL = first byte of myBytes.
a. AL = 10h

3. mov al, [esi+3]


AL = fourth byte of myBytes.
b. AL = 40h
4. mov esi, OFFSET myWords + 2
ESI now points to the third byte of myWords.
myWords = [8Ah, 3Bh, 72h, 44h, 66h]
Third byte = 72h (72h = low byte, 44h = high byte).

5. mov ax, [esi]


AX = 72h44h (little-endian format).
c. AX = 4472h

6. mov edi, 8
EDI = 8.

7. mov edx, [myDoubles + edi]


Access myDoubles[8 / 4] = myDoubles[2] (DWORD, so 4 bytes each).
myDoubles = [1, 2, 3, 4, 5]
myDoubles[2] = 3.
d. EDX = 3

8. mov edx, myDoubles[edi]


Same as above.
e. EDX = 3

9. mov ebx, myPointer


EBX = address of myDoubles.

10. mov eax, [ebx+4]


Access myDoubles[1] (DWORD, offset by 4).
myDoubles[1] = 2.
f. EAX = 2

---

Question 6:

mov esi, OFFSET myBytes


mov ax, [esi] ; a. AX = ?
mov ax, DWORD PTR myWords ; b. AX = ?
mov esi, myPointer
mov ax, [esi+2] ; c. AX = ?
mov ax, [esi+6] ; d. AX = ?
mov ax, [esi-4] ; e. AX = ?

Solution:

1. mov esi, OFFSET myBytes


ESI now points to the myBytes array.

2. mov ax, [esi]


AX = first two bytes of myBytes.
a. AX = 2010h

3. mov ax, DWORD PTR myWords


AX = first WORD of myWords.
b. AX = 3B8Ah

4. mov esi, myPointer


ESI = address of myDoubles.

5. mov ax, [esi+2]


Access second byte of myDoubles[0].
myDoubles[0] = 1 (DWORD = 00000001h).
Second byte = 0000h.
c. AX = 0000h

6. mov ax, [esi+6]


Access third DWORD in myDoubles.
myDoubles[2] = 3.
d. AX = 0003h

7. mov ax, [esi-4]


This refers to the data before myDoubles. Depends on memory context.
e. Undefined unless specified.
Here are the answers for Section 4.5.6:

1. (True/False): A JMP instruction can only jump to a label inside the current
procedure.
False – A JMP instruction can jump to any valid address, whether it’s within the current
procedure or not. It can jump to any label in the program.
2. (True/False): JMP is a conditional transfer instruction.
False – A JMP instruction is an unconditional jump. It always transfers control to the
specified address without any condition.
3. If ECX is initialized to zero before beginning a loop, how many times will the LOOP
instruction repeat? (Assume ECX is not modified by any other instructions inside
the loop.)
Zero times – The LOOP instruction checks if ECX is non-zero. Since ECX is initialized to
zero, it will not repeat (the loop will not execute).
4. (True/False): The LOOP instruction first checks to see whether ECX is not equal to
zero; then LOOP decrements ECX and jumps to the destination label.
False – The LOOP instruction first decrements ECX and then checks if ECX is non-zero. If
it’s non-zero, it will jump to the label.
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 – The LOOP instruction decrements ECX and if ECX is not zero, it will jump to the
destination label. If ECX is zero, it will exit the loop.
6. In real-address mode, which register is used as the counter by the LOOP
instruction?
CX – In real-address mode, the LOOP instruction uses the CX register as the counter.
7. In real-address mode, which register is used as the counter by the LOOPD
instruction?
DX – In real-address mode, the LOOPD instruction uses the DX register as the counter.
8. (True/False): The target of a LOOP instruction must be within 256 bytes of the
current location.
True – In real-address mode, the LOOP instruction can only jump to addresses within 256
bytes of the current instruction.
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

 Outer loop (ECX = 10):


o Inner loop (ECX = 5):
 The inner loop will execute 5 times.
 Each time, eax is incremented by 5. Starting from 3, the values of eax will be:
 First iteration: eax = 3 + 5 = 8
 Second iteration: eax = 8 + 5 = 13
 Third iteration: eax = 13 + 5 = 18
 Fourth iteration: eax = 18 + 5 = 23
 Fifth iteration: eax = 23 + 5 = 28
o After the inner loop, eax = 28.
o The outer loop then decrements ecx and repeats.
 Outer loop (continued):
o The inner loop runs 5 times for each of the 10 outer loop iterations.
o After the inner loop runs 5 times (with eax ending at 28), it runs again for the second
iteration.
o The final value of eax will be:
 After the first iteration: eax = 28
 After the second iteration: eax = 28 + 5 = 33
 After the third iteration: eax = 33 + 5 = 38
 After the fourth iteration: eax = 38 + 5 = 43
 After the fifth iteration: eax = 43 + 5 = 48
 After the sixth iteration: eax = 48 + 5 = 53
 After the seventh iteration: eax = 53 + 5 = 58
 After the eighth iteration: eax = 58 + 5 = 63
 After the ninth iteration: eax = 63 + 5 = 68
 After the tenth iteration: eax = 68 + 5 = 73

So, the final value of EAX is 73.

10. Revise the code from the preceding question so the outer loop counter is not erased when the
inner loop starts.

To preserve the outer loop counter (ECX) across inner loop iterations, we can save its value
before the inner loop and restore it afterward:

mov eax, 0
mov ecx, 10 ; outer loop counter
L1:
push ecx ; save outer loop counter
mov eax, 3
mov ecx, 5 ; inner loop counter
L2:
add eax, 5
loop L2 ; repeat inner loop
pop ecx ; restore outer loop counter
loop L1 ; repeat outer loop

Now, the ECX register (outer loop counter) is preserved by pushing it onto the stack before the
inner loop and popping it back afterward, ensuring that the outer loop counter is not overwritten.
Final Answers:

1. False
2. False
3. Zero times
4. False
5. True
6. CX
7. DX
8. True
9. Final value of EAX = 73
10. Revised code with preserved outer loop counter provided above.

4.6.4 Section Review

1. True/False: Moving a constant value of 0FFh to the RAX register clears bits 8
through 63.

Answer: True

2. True/False: A 32-bit constant may be moved to a 64-bit register, but 64-bit


constants are not permitted.

Answer: False

3. What value will RCX contain after executing the following instructions?
mov rcx, 1234567800000000h
sub ecx, 1

Answer: 12345677FFFFFFFFh

4. What value will RCX contain after executing the following instructions?
mov rcx, 1234567800000000h
add rcx, 0ABABABABh

Answer: 1234568CABABABABh

5. What value will AL contain after executing the following instructions?


mov rdi, OFFSET bArray
dec BYTE PTR [rdi+1]
inc rdi
mov al, [rdi]

Answer: 30h

6. What value will RCX contain after executing the following instructions?
mov rcx, 0DFFFh
mov bx, 3
add cx, bx

Answer: 0E002h

Exercise

1. What will be the value in EDX after each of the lines marked (a) and (b)
execute?
.data
one WORD 8002h
two WORD 4321h
.code
mov edx, 21348041h
movsx edx, one ; (a)
movsx edx, two ; (b)

Answer:

 After (a): FFFF8002h


 After (b): 00004321h

2. What will be the value in EAX after the following lines execute?
mov eax, 1002FFFFh
inc ax

Answer:

 EAX: 10010000h
3. What will be the value in EAX after the following lines execute?
mov eax, 30020000h
dec ax

Answer:

 EAX: 3001FFFFh

4. What will be the value in EAX after the following lines execute?
mov eax, 1002FFFFh
neg ax

Answer:

 EAX: 10010001h

5. What will be the value of the Parity flag after the following lines execute?
mov al, 1
add al, 3

Answer:

 Parity flag: 0

6. What will be the value of EAX and the Sign flag after the following lines
execute?
mov eax, 5
sub eax, 6

Answer:

 EAX: FFFFFFFFFFFFFFF9h
 Sign flag: 1
7. In the following code, the value in AL is intended to be a signed byte. Explain
how the Overflow flag helps, or does not help you, to determine whether the final
value in AL falls within a valid signed range.
mov al, -1
add al, 130

Answer:

 The Overflow flag will be set because adding 130h to -1 results in a value that is outside the
signed byte range (-128 to 127).

8. What value will RAX contain after the following instruction executes?
mov rax, 44445555h

Answer:

 RAX: 44445555h

9. What value will RAX contain after the following instructions execute?
.data
dwordVal DWORD 84326732h
.code
mov rax, 0FFFFFFFF00000000h
mov rax, dwordVal

Answer:

 RAX: 84326732h

10. What value will EAX contain after the following instructions execute?
.data
dVal DWORD 12345678h
.code
mov ax, 3
mov WORD PTR dVal+2, ax
mov eax, dVal

Answer:
 EAX: 12340378h

11. What will EAX contain after the following instructions execute?
.data
.dVal DWORD ?
.code
mov dVal, 12345678h
mov ax, WORD PTR dVal+2
add ax, 3
mov WORD PTR dVal, ax
mov eax, dVal

Answer:

 EAX: 12567B78h

12. (Yes/No): Is it possible to set the Overflow flag if you add a positive integer to
a negative integer?

Answer:

 Yes

13. (Yes/No): Will the Overflow flag be set if you add a negative integer to a
negative integer and produce a positive result?

Answer:

 Yes

14. (Yes/No): Is it possible for the NEG instruction to set the Overflow flag?

Answer:

 Yes
15. (Yes/No): Is it possible for both the Sign and Zero flags to be set at the same
time?

Answer:

 No

16. For each of the following statements, state whether or not the instruction is
valid:

 a. mov ax, var1?


 b. mov ax, var2
 c. mov eax, var3
 d. mov var2, var3
 e. movzx ax, var2
 f. movzx var2, al
 g. mov ds, ax
 h. mov ds, 1000h

Answer:

 a. Invalid. var1 is a signed byte (SBYTE), and AX is 16-bit, which can’t directly handle signed
bytes.
 b. Valid. var2 is a 16-bit value, which fits into AX.
 c. Valid. var3 is a 16-bit signed word (SWORD), and EAX can handle this.
 d. Valid. You can move a SWORD (signed 16-bit) into a WORD (16-bit unsigned) directly.
 e. Valid. movzx can zero-extend a WORD into a WORD register.
 f. Invalid. You can’t move AL (8-bit) into var2 (16-bit).
 g. Valid. MOV ds, ax loads the ds segment register with AX.
 h. Valid. You can move an immediate value (1000h) into the ds register.

17. What will be the hexadecimal value of the destination operand after each of
the following instructions execute in sequence?
mov al, var1 ; (a)
mov ah, [var1+3] ; (b)

Answer:

 (a): The value of var1 is -4, -2, 3, 1. AL will be -4 (0xFC).


 (b): var1+3 is the value 1, so AH will be 01h.
18. What will be the value of the destination operand after each of the following
instructions execute in sequence?
mov ax, var2 ; (a)
mov ax, [var2+4] ; (b)
mov ax, var3 ; (c)
mov ax, [var3-2] ; (d)

Answer:

 (a): AX will be 1000h (from var2).


 (b): AX will be 3000h (from var2+4).
 (c): AX will be FFFFh (from var3).
 (d): AX will be Undefined (from var3-2).

19. What will be the value of the destination operand after each of the following
instructions execute in sequence?
mov edx, var4 ; (a)
movzx edx, var2 ; (b)
mov edx, [var4+4] ; (c)
movsx edx, var1 ; (d)

Answer:

 (a): EDX will hold 1 (from var4).


 (b): EDX will hold 1000h (from var2).
 (c): EDX will hold 3000h (from var4+4).
 (d): EDX will hold FFFF8002h (from var1, sign-extended).

These answers cover all the questions with the simplest and most direct explanations. Let me
know if you need more details!

You might also like