CS401 – Computer Architecture and Assembly Language Programming: Midterm Important Subjective (38 Solved Q&As)
Computer Science • Read every solved subjective question with its model answer directly on this page. Free, no login required.
All 664 Midterm Packs · CS401 Course Page · CS401 MCQ Pack (283 MCQs)
-
Why BP (Base Pointer) default segment is SS (Stack Segment)?
Topic: Registers, Segments & Addressing
BP is used to access the parameters passed to a subroutine and the subroutine's local variables, and both of these are stored on the stack. Since the stack resides in the Stack Segment, BP is associated with SS by default, so an address such as [bp+4] automatically refers to the stack frame (SS:BP) without needing a segment override prefix. In contrast, BX, SI and DI are used for data in the Data Segment, so their default segment is DS.
-
What does the instruction "lds si, [bp+4]" do?
Topic: Registers, Segments & Addressing
LDS loads a far (segment:offset) pointer from memory into a register pair. Here the pointer is a parameter that was passed on the stack: the word at [bp+4] (the offset part) is loaded into SI and the word at [bp+6] (the segment part) is loaded into DS. The result is that DS:SI points to the first string whose address was passed to the subroutine. (Source answer in Team Hadi File 3: "point ds:si to the first string" — completed here.)
-
How is the physical address of a memory location calculated from a segment:offset pair? Explain with the help of an example.
Topic: Registers, Segments & Addressing
In the 8088 segmented memory model every logical address consists of a 16-bit segment and a 16-bit offset. The processor multiplies the segment by 16 (shifts it left by 4 bits, i.e. appends a zero in hex) and adds the offset to get the 20-bit physical address: Physical Address = Segment x 10h + Offset. Example: for the pair A4FB:4872, physical address = A4FB0 + 04872 = A9822h.
-
Write a single instruction that can be used as an alternative of the following two instructions: DEC CX / JNZ NextChar
Topic: Stack, Subroutines & Control Transfer
LOOP NextChar. The LOOP instruction does exactly this: it decrements CX and jumps to the given label if CX has not become zero.
-
What are the instructions used by the Assembly language for Permanent diversion and temporary diversion?
Topic: Stack, Subroutines & Control Transfer
Permanent diversion is provided by the JMP instruction: control is transferred to the target instruction and execution continues from there; no return address is saved, so control never comes back automatically. Temporary diversion is provided by the CALL instruction: before transferring control to the subroutine it pushes the return address (IP, and CS as well for a far call) onto the stack, and the RET instruction at the end of the subroutine pops that address back so execution resumes at the instruction following the CALL. (A closely related question, asking only about temporary diversion, also appears in Team Hadi File 3 — see the next section.)
-
In case of branching techniques used in Intel's architecture, which instruction is used to provide temporary diversion and how?
Topic: Stack, Subroutines & Control Transfer
The CALL instruction is used for temporary diversion. When a CALL is executed, the processor first saves the return address on the stack: for a near call only IP (the offset of the instruction following the CALL) is pushed, while for a far call CS is pushed first and then IP. It then loads IP (and CS for a far call) with the starting address of the subroutine, so execution diverts to the subroutine. The diversion is temporary because the subroutine ends with RET (or RETF), which pops the saved IP (and CS) back from the stack, and the program continues from the instruction immediately after the CALL as if nothing had happened.
-
How the value of Instruction Pointer register is saved and updated when a CALL instruction is executed?
Topic: Stack, Subroutines & Control Transfer
When CALL is executed, IP at that moment already points to the instruction following the CALL, and this value is saved by pushing it onto the stack (SP is decremented by 2 and IP is stored at SS:SP). For a far call, CS is pushed first and then IP. IP is then updated (loaded) with the offset address of the first instruction of the subroutine — for a far call CS is also loaded with the subroutine's segment — so the processor starts executing the subroutine. On RET the saved value is popped back into IP.
-
What is wrong with this program? ; bubble sort subroutine taking parameters from stack / [org 0x0100] (Note: only the first two lines of the program are visible in the source screenshot; the rest of the code is cut off in the source file.)
Topic: Stack, Subroutines & Control Transfer
What is wrong is that the program begins with the subroutine itself. Execution of a .COM program starts at offset 0x0100, so the processor starts executing the bubble sort subroutine directly, as if it were the main program. There is no caller: nothing has pushed the parameters that this subroutine expects to receive from the stack, and no return address has been pushed either, so when the subroutine reaches its RET it will pop a garbage value as the return address and control will jump to an unpredictable location, crashing the program. A subroutine can never be the starting point of a program; it must be placed out of the execution path (e.g. after the exit code or skipped over with a JMP) and must be invoked through CALL after its parameters have been pushed on the stack.
-
What is the difference between a Near CALL and a Far CALL?
Topic: Stack, Subroutines & Control Transfer
A near call is made to a subroutine located in the same code segment: only IP is pushed on the stack as the return address, and it is matched by a simple RET. A far call is made to a subroutine in a different code segment: both CS and IP are pushed on the stack (CS first, then IP), IP and CS are both loaded with the new address, and the subroutine must end with RETF so that both values are popped back.
-
What is the effect of the PUSH and POP instructions on the Stack Pointer (SP)?
Topic: Stack, Subroutines & Control Transfer
PUSH first decrements SP by 2 and then stores the 16-bit operand at the new SS:SP location, so the stack grows downward (towards lower addresses). POP does the reverse: it loads the 16-bit value stored at SS:SP into the destination and then increments SP by 2. Only whole words (16 bits) can be pushed or popped on the 8088 stack; a single byte cannot be pushed or popped.
-
Which interrupts are generated as a result of: 1. Dividing a number by zero 2. If the trap flag is set
Topic: Interrupts
1. Dividing a number by zero (or a divide overflow) generates Interrupt 0 — the divide error interrupt. 2. If the trap flag (TF) is set, Interrupt 1 — the single-step interrupt — is generated after the execution of each instruction. (See also the File 4 question on INT 1 below.)
-
For what purpose "INT 1" is reserved?
Topic: Interrupts
INT 1 is reserved for single-stepping, i.e. program debugging and tracing. When the Trap Flag (TF) in the flags register is set, the processor automatically generates INT 1 after executing each single instruction. A debugger uses this interrupt to gain control after every instruction so that it can display registers and memory and let the programmer execute the program one instruction at a time.
-
Into how many classes are interrupts divided in the 8088 architecture? Name and briefly describe them.
Topic: Interrupts
Interrupts in the 8088 are divided into two classes. (1) Internal interrupts: these are generated from inside the processor — by an exceptional condition such as divide by zero (INT 0), by the single-step trap flag (INT 1), or deliberately by the INT instruction in the software. (2) External (hardware) interrupts: these are generated by devices outside the processor and are signalled through the INTR and NMI pins, e.g. keyboard and timer interrupts.
-
How 16-bit DIV operation is different from 32-bit DIV operation?
Topic: Arithmetic, Division & Bit Operations
In the 16-bit DIV operation the divisor is an 8-bit operand and the implied dividend is the 16-bit value in AX; after division the quotient is stored in AL and the remainder in AH. In the 32-bit DIV operation the divisor is a 16-bit operand and the implied dividend is the 32-bit value formed by the DX:AX pair (DX holds the higher word); after division the quotient is stored in AX and the remainder in DX.
-
What is Divide Overflow Error? Complete the following code such that it will cause Divide Overflow Error. mov BL, ___ / mov AX, ___ / DIV BL
Topic: Arithmetic, Division & Bit Operations
Divide overflow error occurs when the result (quotient) of a division is too large to fit in the destination register — for an 8-bit divisor the quotient must fit in AL (maximum 255), and for a 16-bit divisor it must fit in AX. When the quotient exceeds this capacity the processor generates the divide error interrupt (INT 0) and the program terminates abnormally. Completed code: mov BL, 1 ; mov AX, 0xFFFF ; DIV BL. Here 65535 is divided by 1, giving the quotient 65535, which cannot fit in the 8-bit AL register, so a divide overflow error occurs. (Any values whose quotient is greater than 255, e.g. BL = 2 and AX = 0xFFFF, will do.)
-
Which of the following are bitwise operators? AND, MOV, ADD, MUL, OR, NOT, SUB, XOR, DIV, ADC
Topic: Arithmetic, Division & Bit Operations
The bitwise (logical) operators in the list are: AND, OR, NOT and XOR. These instructions operate on the individual bits of their operands. The others are not bitwise operators: MOV is a data transfer instruction, ADD, SUB and ADC are arithmetic instructions, and MUL and DIV are the multiply and divide instructions.
-
Write down the procedure to clear the selective bit.
Topic: Arithmetic, Division & Bit Operations
To clear a selected bit, AND the value with a mask in which the bit to be cleared is 0 and all other bits are 1. ANDing a bit with 0 forces it to 0, while ANDing the remaining bits with 1 leaves them unchanged. For example, to clear bit 3 of AL: AND AL, 11110111b (0F7h). (This is the procedure given in the typed answer in Team Hadi File 3, cleaned up.)
-
Suppose AL contains 11001011b and CF = 1. Give the new contents of AL after each of the following instructions is executed. Assume the preceding initial conditions for each part of this question. (Unlabelled part) SHL AL, 1; a. SHR AL, 1; b. ROL AL, CL (if CL contains 2); c. SAR AL, CL (if CL contains 2); d. RCR AL, CL (if CL contains 3)
Topic: Arithmetic, Division & Bit Operations
Starting value for every part: AL = 11001011b, CF = 1. SHL AL, 1: every bit moves one position left, the leftmost bit (1) goes into CF and a 0 enters from the right, so AL = 10010110b (CF = 1). a. SHR AL, 1: every bit moves one position right, a 0 enters from the left and the rightmost bit (1) goes into CF, so AL = 01100101b (CF = 1). b. ROL AL, CL (2 rotations): the bits rotate left twice within AL itself, 11001011 -> 10010111 -> 00101111, so AL = 00101111b. c. SAR AL, CL (2 shifts): the bits shift right but the sign bit (1) is copied into the vacated positions, 11001011 -> 11100101 -> 11110010, so AL = 11110010b. d. RCR AL, CL (3 rotations): AL rotates right through the carry flag, treating CF:AL as a 9-bit value: 1|11001011 -> 11100101 (CF=1) -> 11110010 (CF=1) -> 11111001 (CF=0), so AL = 11111001b.
-
What is the difference between the DIV and IDIV instructions?
Topic: Arithmetic, Division & Bit Operations
DIV performs unsigned division: both the dividend and the divisor are treated as unsigned numbers. IDIV performs signed division: the operands are treated as signed numbers in two's complement form, the quotient carries the proper sign, and the remainder takes the sign of the dividend. The implied dividend and the quotient/remainder registers are the same for both instructions.
-
How string instruction work on a block of data?
Topic: String Instructions & Video Memory
In their primitive form the string instructions operate on only one cell (a single byte or word) of memory at a time: the source is always at DS:SI and the destination at ES:DI, and after each operation SI and DI are automatically incremented (if DF = 0) or decremented (if DF = 1) to point to the next cell. To process a whole block of data, the instruction is combined with the REP prefix with the block length loaded in CX; the processor then repeats the instruction CX times, moving through the block cell by cell until CX becomes zero.
-
What will be the affect of executing REPE and REPNE instructions on zero flag? With which instructions you can use both of these REPE and REPNE prefixes?
Topic: String Instructions & Video Memory
REPE and REPNE do not change the zero flag themselves; rather, the repetition is controlled by the ZF set by the string instruction after each step. With REPE (repeat while equal), the instruction repeats as long as the compared elements are equal, i.e. while ZF = 1, and it stops as soon as a mismatch makes ZF = 0 (or CX reaches zero) — so after REPE finishes, ZF = 0 indicates that a mismatch was found, while ZF = 1 means all the compared elements were equal. With REPNE (repeat while not equal), the instruction repeats while the elements are unequal, i.e. while ZF = 0, and stops when a match makes ZF = 1 (or CX reaches zero) — so ZF = 1 at the end indicates that a match was found. Both REPE and REPNE prefixes are meaningful only with the CMPS and SCAS instructions, because these are the only string instructions that affect the flags; with MOVS, LODS and STOS only the plain REP prefix is used.
-
Write an assembly language program that clears the computer screen.
Topic: String Instructions & Video Memory
The screen is cleared by filling the whole video memory (at segment B800) with the space character (ASCII 20h) having the normal attribute byte 07h, using REP STOSW for all 80 x 25 = 2000 screen locations: [org 0x0100] mov ax, 0xb800 mov es, ax mov di, 0 mov ax, 0x0720 mov cx, 2000 cld rep stosw mov ax, 0x4c00 int 0x21. Here AX = 0720h places the attribute 07 in AH and the space character 20 in AL, STOSW stores one character-plus-attribute word at ES:DI and advances DI by 2, and REP repeats it CX (2000) times, blanking the entire screen.
-
Following instructions are taken from the program of printing a "hello word" at desired screen location. What functionality is achieved by the following code lines? [Initially SI is pointing to the string and DI is pointing to the top left column of the screen] mov al, [si] / mov [es:di], ax / add di, 2 / add si, 1
Topic: String Instructions & Video Memory
Together these lines copy one character of the string onto the screen and advance both pointers, so that repeated execution (in a loop) prints the whole string. Line by line: mov al, [si] loads the character of the string pointed to by DS:SI into AL (AH already contains the attribute byte). mov [es:di], ax stores the complete word — the character in AL together with its attribute in AH — into video memory at ES:DI, which displays the character at the screen position pointed to by DI (initially the top-left corner). add di, 2 moves DI to the next screen location, because each screen cell occupies two bytes (character + attribute). add si, 1 moves SI to the next character of the string (the last line is visible in the scrolled view of the same question in the source).
-
Explain the format of the attribute byte used for each character in video memory.
Topic: String Instructions & Video Memory
Every screen location occupies two bytes in video memory: the first (lower) byte is the ASCII code of the character and the second (higher) byte is its attribute byte, which controls how the character is displayed. In the attribute byte, bits 0-2 give the foreground (character) colour as RGB bits, bit 3 is the foreground intensity bit (bright/dim), bits 4-6 give the background colour as RGB bits, and bit 7 is the blinking bit. For example, attribute 07h means a normal white character on a black background.
-
Differentiate between the LODS, STOS and MOVS string instructions.
Topic: String Instructions & Video Memory
LODS (load string) copies the byte or word at DS:SI into AL or AX. STOS (store string) copies the contents of AL or AX into the location at ES:DI. MOVS (move string) copies the byte or word directly from the source at DS:SI to the destination at ES:DI without involving the accumulator. After execution, LODS updates only SI, STOS updates only DI, and MOVS updates both SI and DI — each is incremented or decremented according to the Direction Flag and the operand size.
-
What does iAPX88 stand for?
Topic: Processor Basics & Registers
iAPX88 stands for Intel Advanced Processor Extensions 88. It is the name of the architecture of the Intel 8088 processor.
-
What are registers?
Topic: Processor Basics & Registers
Registers are the storage cells inside the processor. They hold the operands and data on which the processor is currently working, so that it does not have to access the slower memory for every operation.
-
What is an opcode in an encoded instruction?
Topic: Processor Basics & Registers
The opcode (operation code) is the part of an encoded machine instruction that tells the processor which operation to perform. For example, in the encoded instruction B80500, B8 is the opcode (move an immediate value into AX) and 0500 is the operand value.
-
In which direction does the memory address travel on the address bus?
Topic: Processor Basics & Registers
The memory address always goes from the processor to the memory. The address bus is unidirectional: the processor places the address of the required memory location on it, and the memory responds on the data bus.
-
What does the stack pointer (SP) contain?
Topic: Stack & Subroutines
The stack pointer contains the address (offset within the stack segment) of the top element of the stack. It is decremented when a value is pushed and incremented when a value is popped.
-
If the values 6, 3, 5, 8, 4, 7 and 9 are pushed onto the stack in this order, which value will come out first when a POP instruction is executed? Why?
Topic: Stack & Subroutines
The value 9 will come out first, because the stack works in LIFO (Last In First Out) manner: the last value pushed onto the stack is the first one to be popped off it.
-
How does the base pointer (BP) access the parameters and the local variables of a subroutine?
Topic: Stack & Subroutines
BP accesses the parameters passed to a subroutine through positive offsets, such as [BP+4], and the local variables of the subroutine through negative offsets, such as [BP-2]. This is possible because BP is associated with the stack segment by default.
-
How is JMP different from JNZ?
Topic: Branching & Control Transfer
JMP is an unconditional jump: the control is always transferred to the target address. JNZ is a conditional jump: the control is transferred only when the zero flag is clear (ZF = 0), that is, when the result of the previous operation was not zero.
-
Suppose the current value of IP is 0x0129 and a jump instruction carries a relative address of 0x0012. What will be the new value of IP after the jump is taken?
Topic: Branching & Control Transfer
In a relative jump the relative address is added to the current value of IP: IP = 0x0129 + 0x0012 = 0x013B.
-
What makes CMP different from SUB?
Topic: Arithmetic & Logic Instructions
SUB is a destructive subtraction: it subtracts the source operand from the destination operand and stores the result back in the destination. CMP is a non-destructive subtraction: it performs the same subtraction only to set the flags, and leaves both operands unchanged. Both instructions affect the flags in exactly the same way.
-
What does the following piece of code do? shl word [multiplicand], 1 rcl word [multiplicand+2], 1
Topic: Arithmetic & Logic Instructions
It performs an extended shift left of the 32-bit multiplicand by one bit. SHL shifts the lower word to the left and the bit leaving it is dropped into the carry flag; RCL then rotates the higher word to the left through the carry, bringing that bit into its lowest position. Together the two instructions shift the complete 32-bit value one position to the left.
-
How are logical errors different from syntax errors?
Topic: Assembler & Data Definition
Identifying syntax errors is the responsibility of the assembler: they are mistakes in the format of the instructions and are reported when the program is assembled. Logical errors are the responsibility of the programmer: the program assembles and runs without any error message, but it produces incorrect results because the logic of the program is wrong.
-
What is the difference between the db and dw directives?
Topic: Assembler & Data Definition
The db (define byte) directive reserves 8 bits, that is one byte, of memory for a variable, while the dw (define word) directive reserves 16 bits, that is one word (two bytes), of memory.