Machine Organization and Assembly Language Programming Assignment
1. Write an assembly program to find the factorial of the first 5 numbers
and display the result on the screen.
2. List and describe addressing modes types with 1 example for each type.
A. Immediate Addressing:
* The operand is directly specified within the instruction itself.
* Example: MOV AX, 5 ; Load the value 5 into register AX
B. Direct Addressing:
* The operand's memory address is given explicitly in the instruction.
* Example: MOV BX, [1000h] ; Load the value stored at memory address
1000h into register BX
C. Register Addressing:
* The operand is located in a CPU register.
* Example: ADD AX, BX ; Add the values in registers AX and BX, store the
result in AX
D. Register Indirect Addressing:
* The operand's memory address is stored in a register.
* Example: MOV CX, [BX] ; Load the value from the memory address
pointed to by register BX into register CX
E. Indexed Addressing:
* The operand's memory address is calculated by adding a base address
(specified in the instruction) to an index value (usually stored in a register).
* Example: MOV DX, [ARRAY + SI] ; Load the value from the element of
array ARRAY whose offset is specified by register SI into register DX
F. Base Register Addressing:
* Similar to indexed addressing, but the base address is stored in a designated
base register.
* Example: MOV AL, [BP + OFFSET] ; Load the value from the memory
location relative to the base address in register BP into register AL
G. Auto-Increment/Decrement Addressing:
* The register used for indirect addressing is automatically incremented or
decremented after the operand is accessed.
* Example: INC [DI] ; Increment the value stored at the memory address
pointed to by register DI, then increment DI itself
H. Implicit Addressing:
* The operand is implicitly specified by the instruction itself, often involving
accumulator registers.
* Example: PUSH AX ; Push the value in register AX onto the stack (implied
operand is the stack pointer register)
I. Relative Addressing:
* The operand's memory address is calculated relative to the current program
counter (PC).
* Example: JMP LOOP ; Jump to a label named "LOOP" relative to the
current instruction
3. Explain data transfer, Arithmetic and logical instruction with example
Data Transfer Instructions:
* Purpose: Move data between registers, memory locations, and I/O ports.
* Examples:
- MOV AX, BX (moves the value in register BX to register AX)
- MOV [1000h], CX (moves the value in register CX to memory address
1000h)
- IN AL, 60h (reads a byte from I/O port 60h into register AL)
- OUT 40h, DX (sends a byte from register DX to I/O port 40h)
Arithmetic Instructions:
* Purpose: Perform arithmetic operations on data.
* Examples:
- ADD AX, BX (adds the values in registers AX and BX, stores the result in
AX)
- SUB CX, DX (subtracts the value in register DX from register CX, stores
the result in CX)
- MUL BX (multiplies the value in register AX by the value in register BX,
stores the 16-bit result in DX:AX)
- DIV CX (divides the value in DX:AX by the value in register CX, stores
the quotient in AX and remainder in DX)
Logical Instructions:
* Purpose: Perform bitwise logical operations on data.
* Examples:
- AND AX, BX (performs a bitwise AND operation on the values in
registers AX and BX, stores the result in AX)
- OR CX, DX (performs a bitwise OR operation on the values in registers
CX and DX, stores the result in CX)
- XOR BX, CX (performs a bitwise XOR operation on the values in registers
BX and CX, stores the result in BX)
- NOT AX (inverts all bits in register AX)
Additional Insights:
* These instruction types form the core of most assembly languages.
* Data transfer instructions are essential for loading data, storing results, and
interacting with I/O devices.
* Arithmetic instructions enable calculations and data manipulation.
* Logical instructions provide fine-grained control over individual bits, often
used for masking, testing, and setting specific bits within data.
4. Explain rotate and shift (Arithmetic and logical) instruction with
example.
Shift Instructions:
* Move bits to the left or right within a register or memory location.
* Types:
- Logical Shifts:
* Bits shifted out are discarded.
* Bits shifted in are zeros.
* Used for unsigned numbers.
- Arithmetic Shifts:
* Bits shifted out are discarded.
* Bits shifted in are copies of the original sign bit.
* Preserves the sign of signed numbers.
Rotate Instructions:
* Circle bits around within a register or memory location.
* Types:
- Rotate Left (ROL):
* Bits shifted out from the left end are moved to the right end.
- Rotate Right (ROR):
* Bits shifted out from the right end are moved to the left end.
Examples:
Logical Shift Left (LSL):
MOV AL, 10110101b ; AL = 181
SHL AL, 2 ; Shift left by 2 bits
; AL becomes 01101000b (64)
Arithmetic Shift Right (SAR):
MOV BL, 11110101b ; BL = -75 (signed)
SAR BL, 3 ; Shift right by 3 bits
; BL becomes 11111110b (-2)
Rotate Left (ROL):
MOV CL, 10011011b ; CL = 155
ROL CL, 1 ; Rotate left by 1 bit
; CL becomes 00110111b (55)
5) For the following given instruction determine the addresses.
DS= 2102h
Offset= 481
Logical Address= 2102:481
Physical Address= 2180h
_
DS:
Value: Unknown
Reason: The code doesn't explicitly set the DS register. Its value depends on
previous instructions or assembly context.
Offset:
In MOV [0018h], 0005h: 0018h (decimal 24)
In MOV AX, [BX+022h]: 022h (decimal 34)
Logical Addresses:
MOV [0018h], 0005h: Depends on DS value. If DS were 1000h, it would be
1000:0018h.
MOV AX, [BX+022h]: Depends on DS and BX values. If DS were 1000h, BX
were 0010h, it would be 1000:0032h.
Physical Addresses:
Cannot be determined without knowing DS
DS:
- Value: Unknown (not explicitly set in the code)
- For this analysis, let's assume DS = 1000h (hypothetical)
Offsets:
- MOV [0022h], 0100h: Offset is 0022h (decimal 34).
- MOV SI, 0013h: No memory access, so no offset.
- MOV AX, [SI + 012h]: Offset is 025h (calculated as 013h + 012h).
Logical Addresses:
- MOV [0022h], 0100h: 1000:0022h
- MOV AX, [SI + 012h]: 1000:0025h
Physical Addresses:
- MOV [0022h], 0100h: 10220h (calculated as 1000h * 10h + 0022h)
- MOV AX, [SI + 012h]: 10250h (calculated as 1000h * 10h + 0025h)
DS:
- Value: Unknown (not explicitly set in the code)
- For this analysis, let's assume DS = 1000h (hypothetical)
Offsets:
- MOV [0033h], 0110h: Offset is 0033h.
- MOV SI, 0011h: No memory access, so no offset.
- MOV BX, 0013h: No memory access, so no offset.
- MOV AX, [BX + SI + 012h]: Offset is 036h (calculated as 0013h + 0011h +
012h).
Logical Addresses:
- MOV [0033h], 0110h: 1000:0033h
- MOV AX, [BX + SI + 012h]: 1000:0036h
Physical Addresses:
- MOV [0033h], 0110h: 10330h (calculated as 1000h * 10h + 0033h)
- MOV AX, [BX + SI + 012h]: 10360h (calculated as 1000h * 10h + 0036h)
6 .Write an assembly language program to print the following patterns:
Name: Yohannes Abera
ID: RCD/2453/2014
Section: J