Assembly Paper Key
Assembly Paper Key
Index Register
Index register is a register in the CPU that can be used to store an offset value.
This offset value can be added to the address of a memory location to access a
different location.
For example, if the index register contains the value 10, and the address of a memory
location is 0x1000, then the effective address of the memory location is 0x1010.
Index registers are often used to access elements of arrays or strings, or to iterate
through a loop.
Q 1 (iI)
The CMP instruction in assembly language compares two operands and sets the status
flags in the CPU accordingly. The operands can be registers, memory locations, or
immediate values.
The cmp instruction subtracts the second operand from the first operand.
It sets the zero flag (ZF) if the result is zero, and the carry flag (CF) if the result is
negative.
Q1 (iii)
proc sum
mov eax, ecx
add eax, edx
ret
endp sum
Q1 (iv)
Push: This operation adds an element to the top of the stack.
Pop: This operation removes and returns the top element from the stack.
Peek (or Top): This operation returns the top element from the stack without removing
it.
Q1 (v)
in assembly language, the db (define byte) directive is used to declare and allocate
memory for one or more bytes, while the dw (define word) directive is used to declare
and allocate memory for one or more words (typically 2 bytes each). When you change
from dw to db, the amount of memory allocated and the way data is stored will be
affected.
Q1 (vi)
the purpose of segmentation registers in older x86 processors was to implement
memory segmentation for memory protection and organization, but their importance has
decreased with the transition to modern memory management techniques like paging.
Q1 (vii)
The "jump near" instructions are used for unconditional or conditional jumps over a
larger range of addresses. They can jump to any location within the current code
segment.
The "jump short" instructions are used for conditional or unconditional jumps within a
short range of addresses. These instructions use a signed 8-bit offset, which means
they can only jump a limited distance forward or backward from the current instruction's
location.
Q1 (viii)
The SAL instruction is used to shift the bits of a value to the left by a specified number
of positions.
The SAR instruction is used to shift the bits of a value to the right by a specified number
of positions.
Q1 (ix)
section .text
global _start
_start:
; Assuming values are already in AX and CX
cmp ax, cx ; Compare the values in AX and CX
jg L2 ; Jump to label L2 if AX > CX
; Continue with the rest of the code
L2:
; Code to execute when AX > CX
; Rest of the program
Q1 (x)
The Zero Flag (ZF) is a status flag in the flags register of a processor that indicates
whether the result of an arithmetic or logical operation is zero. It's a single bit that is set
to 1 when the result of an operation is zero, and it's cleared (set to 0) when the result is
non-zero.
The purpose of the Zero Flag is to facilitate conditional branching in program execution.
By checking the Zero Flag after an operation, you can make decisions in your program
based on whether the result was zero or not.
Q1 (xi)
The DUP operator, short for "duplicate," is a directive used in assembly languages and
some high-level programming languages to create multiple instances of the same value
or data structure. It's often used to define arrays, strings, or other repeated data
elements. The number of duplications is specified as part of the directive.
Q1 (xii)
In x86 assembly language, interrupt instructions are used to trigger software interrupts,
which are a way of interacting with the operating system or supervisor routines. The INT
instruction is used to initiate an interrupt with a specified interrupt vector. Here's the
basic syntax of the INT instruction:
INT interrupt_number
interrupt_number: This is the number of the interrupt or interrupt vector you want to call.
Different interrupt numbers correspond to different services provided by the operating
system or the BIOS.
Section B
Q1
section .data
newline db 10 ; ASCII code for newline character
section .text
global _start
_start:
mov ecx, 1 ; Initialize counter (outer loop)
outer_loop:
mov ebx, ecx ; Copy the value of counter to ebx (inner loop)
inc ebx ; Increment ebx by 1 (for printing)
inner_loop:
mov al, bl ; Copy value from ebx to al
add al, 30h ; Convert the value to ASCII character
mov ah, 0x0E ; BIOS teletype function
Q2
Q3
section .text
global _start
_start:
; Assuming values are already in registers eax and y
; Compare eax with 0
cmp eax, 0
jne not_zero ; Jump if not equal to 0
; Compare y with 1
cmp y, 1
jle not_zero ; Jump if less than or equal to 1
; If both conditions are met, y = y - 1
dec y
jmp end
not_zero:
; If any of the conditions is not met, y = y + 1
inc y
end:
Q4
section .data
seed dd 12345 ; Initial seed value for LCG
multiplier dd 1664525 ; Multiplier for LCG
increment dd 1013904223 ; Increment for LCG
modulus dd 4294967296 ; Modulus (2^32) for LCG
section .text
global _start
_start:
; Generate a pseudorandom number
mov eax, [seed]
mov edx, [multiplier]
mul edx ; EAX * multiplier
add eax, [increment] ; EAX + increment
xor edx, edx ; Clear EDX
div dword [modulus] ; EAX / modulus, result in EAX
Q5
Q6
section .data
a dd 1
b dd 2
c dd 3
d dd 4
e dd 5
f dd 10
g dd 20
h dd 30
i dd 40
section .bss
x resd 1 ; Reserve space for the result 'x'
section .text
global _start
_start:
; Calculate e/f
fild dword [e] ; Load e as a floating-point number
fidiv dword [f] ; Divide by f
; Calculate g-h+i
fild dword [g] ; Load g as a floating-point number
fsub dword [h] ; Subtract h
fiadd dword [i] ; Add i
Q7
section .data
input db "Hello", 0
output db 5 dup(0) ; Reserve space for reversed string
section .bss
len resb 1 ; Variable to store string length
section .text
global _start
_start:
; Calculate the length of the input string
mov esi, input
xor ecx, ecx ; Clear ECX for string length calculation
.loop:
mov al, byte [esi]
cmp al, 0
je .found_length
inc esi
inc ecx
jmp .loop
.found_length:
mov [len], cl ; Store the string length
.reverse_loop:
dec esi
mov al, byte [esi]
mov [edi], al
dec edi
cmp esi, offset input
jge .reverse_loop