Assembly Language, Past Papers, UOS Main Campus

Computer Organization & Assembly Language Past Paper 2014 – BSCS 4th Term UOS (CMP-223)

ProgramBSCS
Term4th
Year2014
Course codeCMP-223
AnswersSolved ✓

University of Sargodha BS Computer Science 4th term Computer Organization & Assembly Language (CMP-223) paper, 2014 exam, session 2012-16 (80 marks, 2:30 hours). The paper is retyped below, followed by solved answers for all 16 short questions and all subjective questions, including tested 8086 programs for nested loops and for printing the even numbers from 1 to 10.

Solved answers

Q1 – Short answers (16 × 2)

1. Stored program concept. The von Neumann idea that program instructions are stored in the same memory as data, in binary, so the CPU fetches and executes them one by one and the program can be changed simply by loading a new one into memory.

2. Four main components. Processor (CPU – ALU and control unit), main memory, input/output modules (devices), and the system interconnection (buses) that connects them.

3. Moore’s law. Gordon Moore (1965) observed that the number of transistors on a chip doubles about every 18–24 months. As a result computers become faster, smaller and cheaper over time.

4. States of instruction execution. Instruction address calculation → instruction fetch → instruction operation decoding → operand address calculation → operand fetch → data operation (execute) → operand store (result), and then the interrupt check before the next instruction.

5. Control unit. The part of the CPU that directs all operations: it fetches and decodes instructions and generates timing and control signals that tell the ALU, registers, memory and I/O what to do.

6. System bus and types. A bus is a shared communication path of wires that connects the CPU, memory and I/O modules. Types: data bus (carries data), address bus (carries the address of the memory/I/O location) and control bus (read, write, interrupt, clock signals). By use: system bus, local bus, expansion buses such as ISA, PCI, USB.

7. PCI slot. A slot on the motherboard for the Peripheral Component Interconnect bus, a high-speed 32/64-bit expansion bus used to attach cards such as network, sound and graphics cards.

8. Stack and its operations. A LIFO area of memory addressed by SS:SP. PUSH decreases SP by 2 and stores a word; POP reads the word and increases SP by 2. It is used for saving registers, return addresses (CALL/RET) and passing parameters.

9. Real time application. An application that must respond to events within a strict time limit, e.g. air-bag control, pacemakers, industrial robots, flight control. A late answer counts as a failure.

10. Peripheral control interrupts. Interrupts generated by peripheral devices or their controllers (keyboard, timer, disk, printer) to tell the CPU that they need service. On the PC they come through the programmable interrupt controller (8259) as hardware interrupts IRQ0–IRQ15.

11. Instruction set. The complete collection of machine instructions a processor can execute (data transfer, arithmetic, logic, control/branch, I/O), together with their formats and addressing modes, e.g. the x86 instruction set.

12. Flags. Bits of the flags register that show the result of the last operation or control the CPU: CF (carry), ZF (zero), SF (sign), OF (overflow), PF (parity), AF (auxiliary carry); control flags IF (interrupt enable), DF (direction), TF (trap/single step).

13. Assembler vs debugger. An assembler translates assembly language source into machine code (object file), e.g. MASM, NASM. A debugger loads a program and lets the programmer run it step by step, set breakpoints and inspect registers and memory to find errors, e.g. DEBUG, CodeView, GDB.

14. Machine vs assembly language. Machine language is binary code that the CPU executes directly (e.g. 10111000 00000101). Assembly language uses mnemonics and symbols (MOV AX, 5) that are easier for people and must be translated by an assembler. Both are machine dependent.

15. Functions in assembly language. Called procedures: a block of code defined with PROC … ENDP, called with CALL (which pushes the return address) and ended with RET. Parameters are passed in registers or on the stack.

16. Program with nested loops. See the program below – an outer loop of 3 rows and an inner loop printing 4 stars, using PUSH/POP to save CX.

        mov cx, 3          ; outer loop: 3 rows
outer:  push cx            ; save outer counter
        mov cx, 4          ; inner loop: 4 stars
inner:  mov dl, '*'
        mov ah, 2
        int 21h
        loop inner         ; inner loop uses CX
        mov dl, 0Dh
        int 21h
        mov dl, 0Ah
        int 21h            ; new line
        pop cx             ; restore outer counter
        loop outer
        mov ah, 4Ch
        int 21h

Q2 – Interrupts

An interrupt is a signal that makes the processor stop the current program, run a special routine (interrupt service routine, ISR) and then return to the program. It lets slow I/O devices work without the CPU waiting for them.

  • Classes: program (overflow, divide by zero, illegal instruction), timer, I/O (device finished or error), hardware failure (power, memory parity). On the 8086: hardware interrupts (INTR, NMI), software interrupts (INT n, e.g. INT 21h for DOS) and exceptions.
  • Interrupt cycle: after each execute cycle the CPU checks for an interrupt; if one is pending and enabled (IF = 1) it saves the flags, CS and IP on the stack, gets the ISR address from the interrupt vector table (type × 4) and jumps there. The ISR saves registers, services the device, restores registers and executes IRET to return.
  • Multiple interrupts: either disable interrupts while one is processed (sequential), or use priorities so a higher-priority device can interrupt a lower one (nested).

Example: a program sends data to a printer. Without interrupts the CPU waits in a loop until the printer is ready. With interrupts the CPU sends one block, continues with other work, and when the printer finishes it raises an interrupt; the ISR sends the next block. The CPU time saved is large because the printer is much slower than the CPU. Software example: MOV AH, 2 / MOV DL, 'A' / INT 21h calls the DOS service that prints A.

Q3 – Addressing modes

An addressing mode is the way an instruction specifies where its operand is. Examples for the 8086:

ModeExampleOperand is
ImmediateMOV AX, 25the constant 25 inside the instruction
RegisterMOV AX, BXin register BX
DirectMOV AX, [1234h]in memory at offset 1234h (DS segment)
Register indirectMOV AX, [BX]in memory at the address held in BX/SI/DI
BasedMOV AX, [BX+4]at BX (or BP) + displacement – fields of a record
IndexedMOV AX, [SI+10]at SI/DI + displacement – array elements
Based-indexedMOV AX, [BX+SI+2]at BX + SI + displacement – 2-D arrays
RelativeJMP labelIP + displacement (jumps)
ImpliedCLC, STCoperand implied by the opcode (carry flag)

Immediate and register modes are fastest (no memory access); direct mode is simple but fixed; indirect, based and indexed modes let one instruction work on different memory locations, which is needed for arrays, pointers and loops.

Q4 – Even numbers from 1 to 10 (8086 assembly)

.model small
.stack 100h
.code
main proc
        mov ax, @data
        mov ds, ax
        mov bl, 2          ; first even number
        mov cx, 5          ; 2, 4, 6, 8, 10 -> 5 numbers
next:   mov al, bl
        xor ah, ah
        mov dh, 10
        div dh             ; AL = tens digit, AH = units digit
        mov bh, ah         ; save units digit
        cmp al, 0
        je units           ; no tens digit for 2..8
        mov dl, al
        add dl, '0'
        mov ah, 2
        int 21h            ; print tens digit
units:  mov dl, bh
        add dl, '0'
        mov ah, 2
        int 21h            ; print units digit
        mov dl, ' '
        int 21h            ; print a space
        add bl, 2          ; next even number
        loop next
        mov ah, 4Ch
        int 21h            ; return to DOS
main endp
end main

Output: 2 4 6 8 10. DIV splits each number into tens and units so that 10 is printed as two digits.

The programs were assembled with NASM (same instructions) and run in an 8086 emulator: output “2 4 6 8 10”, “1 3 5 7 9” and three rows of ****.

Q5 – Direct Memory Access

DMA lets an I/O device transfer a block of data directly to or from memory without the CPU moving each word. The CPU only tells the DMA module: read or write, the I/O device address, the starting memory address and the number of words. The DMA module then transfers the whole block and sends an interrupt to the CPU when it is finished.

When the DMA module takes control of the bus it uses cycle stealing: it asks for the bus (bus request / HOLD), the CPU finishes its current bus cycle, grants the bus (HLDA) and is suspended for one bus cycle while a word is transferred. While DMA holds the bus the processor does not use the bus: it can continue internal work that needs no memory access (operations on registers, decoding, work on data already in the cache or the prefetch queue); it simply waits whenever it needs the bus. It is not interrupted and does not save its context – it just pauses for a bus cycle. When the block is complete the DMA module interrupts the CPU.

Configurations: single-bus detached DMA, single-bus integrated DMA–I/O, and a separate I/O bus. DMA is much faster than programmed or interrupt-driven I/O for large blocks (disk, network, sound).

Q6 – Variable-length instruction format

Advantages: instructions use only as many bytes as they need, so programs are smaller and use memory and cache better; it is easy to provide many opcodes, many addressing modes and large immediate values/addresses; new instructions can be added later (as in x86).

Disadvantages: decoding is more complex and slower because the CPU must first find the length of each instruction; fetching may cross word or cache-line boundaries; pipelining and parallel decoding of several instructions are harder; the control unit needs more hardware. Fixed-length formats (RISC, e.g. 32-bit MIPS/ARM) are easier to fetch, decode and pipeline but waste space.

Q7 – Register organization of a processor

Intel 8086 registers (all 16-bit):

  • General purpose: AX (accumulator – arithmetic, I/O, MUL/DIV), BX (base – addressing memory), CX (count – LOOP, shifts, string repeat), DX (data – I/O port, high word of MUL/DIV). Each can be used as two 8-bit halves: AH/AL, BH/BL, CH/CL, DH/DL.
  • Pointer and index: SP (stack pointer), BP (base pointer – stack frames), SI (source index), DI (destination index) – used for string and array operations.
  • Segment registers: CS (code), DS (data), SS (stack), ES (extra). Physical address = segment × 16 + offset (20-bit address, 1 MB).
  • Instruction pointer: IP – offset of the next instruction in CS.
  • Flags register: status flags CF, PF, AF, ZF, SF, OF and control flags TF, IF, DF.

Registers are visible to the programmer (user-visible) or used by the CPU internally (control and status registers such as MAR, MBR, IR, PC/IP and the PSW). Keeping data in registers avoids slow memory accesses.

Save this paper for offline studyDownload the question paper, or save the paper together with all solved answers as a PDF.
↓ Download Paper (JPG)

About Saif R

An educator and technology enthusiast passionate about empowering students through quality education and digital learning. Dedicated to making academic resources and opportunities accessible to everyone.