.MODEL SMALL
.STACK 100H
.DATA
MSG DB 0AH, 0DH, "ENTER A NUMBER TO SQUARE IT: $"
OUT1 DB 0AH, 0DH, "SQUARE OF $"
OUT2 DB " IS $"
QUIT DB 0AH, 0DH, "CONTINUE? Y FOR YES ELSE FOR NO: $"
.CODE
MAIN:
MOV AX, @DATA
MOV DS, AX
AGAIN:
LEA DX, MSG
MOV AH, 09H
INT 21H
MOV AH, 01H
INT 21H
PUSH AX
CMP AL, 39H
JG AGAIN
CMP AL, 30H
JL AGAIN
PUSH AX
SUB AL, 30H
MOV BL, AL
MUL BL
AAM
MOV BX, AX
LEA DX, OUT1
MOV AH, 09H
INT 21H
POP DX
MOV AH, 02H
INT 21H
LEA DX, OUT2
MOV AH, 09H
INT 21H
MOV DL, BH
ADD DL, 30H
MOV AH, 02H
INT 21H
MOV DL, BL
ADD DL, 30H
MOV AH, 02H
INT 21H
LEA DX, QUIT
MOV AH, 09H
INT 21H
MOV AH, 01H
INT 21H
OR AL, 20H
CMP AL, 'y'
JE AGAIN
MOV AH, 04CH
INT 21H
END MAIN
output:
Enter a number to square it: 6
squre og 6 is 36
continue? y for yes else for no:
This Assembly Language Program (ALP) is designed to find the square of a single-digit number entered by the user and display the result. The program also asks if the user wants to continue and allows them to repeat the process if desired. The program is written for the x86 architecture and uses DOS interrupts for input and output operations.
Detailed Explanation
Sections and Directives
1. MODEL SMALL
- Specifies the memory model. The small model means the code and data will fit within 64 KB each.
2. STACK 100H
- Allocates 256 bytes (100H) for the stack.
3. DATA
- This section defines initialized data and messages to be used in the program.
```asm
MSG DB 0AH, 0DH, "ENTER A NUMBER TO SQUARE IT: $"
OUT1 DB 0AH, 0DH, "SQUARE OF $"
OUT2 DB " IS $"
QUIT DB 0AH, 0DH, "CONTINUE? Y FOR YES ELSE FOR NO: $"
```
4. CODE
- This section contains the executable code of the program.
```asm
.CODE
```
MAIN Program
1. Initialization
- Sets up the data segment.
```asm
MAIN:
MOV AX, @DATA
MOV DS, AX
```
2. Main Loop (AGAIN)
- Displays the prompt message to enter a number.
```asm
AGAIN:
LEA DX, MSG
MOV AH, 09H
INT 21H
```
3. Input a Number
- Reads a single character (number) from the user.
```asm
MOV AH, 01H
INT 21H
```
4. Validation
- Checks if the input character is a valid digit ('0' to '9').
asm
CMP AL, 39H
JG AGAIN
CMP AL, 30H
JL AGAIN
```
5. Conversion and Calculation
- Converts the ASCII character to its numeric value and calculates the square.
```asm
SUB AL, 30H ; Convert ASCII to decimal
MOV BL, AL ; Move the number to BL
MUL BL ; Multiply AL by BL, result in AX
AAM ; Adjust AX to unpack BCD (AL = low, AH = high)
```
6. Display Result
- Outputs the result in a formatted manner.
```asm
LEA DX, OUT1
MOV AH, 09H
INT 21H
POP DX ; Retrieve the original input number
MOV AH, 02H
INT 21H
LEA DX, OUT2
MOV AH, 09H
INT 21H
MOV DL, BH ; Output high nibble
ADD DL, 30H ; Convert to ASCII
MOV AH, 02H
INT 21H
MOV DL, BL ; Output low nibble
ADD DL, 30H ; Convert to ASCII
MOV AH, 02H
INT 21H
```
7. Prompt for Continuation
- Asks if the user wants to continue.
asm
LEA DX, QUIT
MOV AH, 09H
INT 21H
MOV AH, 01H
INT 21H
OR AL, 20H ; Convert to lowercase
CMP AL, 'y'
JE AGAIN
```
8. **Exit Program**
- Exits the program gracefully.
```asm
MOV AH, 04CH
INT 21H
END MAIN
```
Summary
- The program initializes the data segment.
- Prompts the user to enter a single-digit number.
- Validates the input.
- Converts the input from ASCII to a numeric value.
- Computes the square of the number.
- Displays the result.
- Prompts the user to continue or exit.
- Exits the program if the user does not want to continue.
This code is a good example of basic input/output operations, looping, and simple arithmetic in assembly language, utilizing DOS interrupts for character-based I/O.
Write an ALP to find the cube of a number
program:
.MODEL SMALL
.STACK 100H
.DATA
MSG DB 0AH, 0DH, "ENTER A NUMBER TO FIND CUBE OF IT: $"
OUT1 DB 0AH, 0DH, "CUBE OF $"
OUT2 DB " IS $"
QUIT DB 0AH, 0DH, "CONTINUE? Y FOR YES ELSE FOR NO: $"
.CODE
MAIN:
MOV AX, @DATA
MOV DS, AX
AGAIN:
LEA DX, MSG
MOV AH, 09H
INT 21H
MOV AH, 01H
INT 21H
PUSH AX
CMP AL, 39H
JG AGAIN
CMP AL, 30H
JL AGAIN
PUSH AX
PUSH AX
SUB AL, 30H
MOV BL, AL
MUL BL
AAM
MOV BX, AX ; BH = 06, BL, 04 ; IF ENTERED 08
POP AX
SUB AL, 30H
MUL BL
AAM
MOV CL, AL ; FIRST RESULT
MOV CH, AH ;CARRY
POP AX
SUB AL, 30H
MOV BL, AL
MOV AL, BH
MUL BL
AAM
ADD AL, CH
AAA
MOV BX, AX
LEA DX, OUT1
MOV AH, 09H
INT 21H
POP DX
MOV AH, 02H
INT 21H
LEA DX, OUT2
MOV AH, 09H
INT 21H
MOV DL, BH
ADD DL, 30H
MOV AH, 02H
INT 21H
MOV DL, BL
ADD DL, 30H
MOV AH, 02H
INT 21H
MOV DL, CL
ADD DL, 30H
MOV AH, 02H
INT 21H
LEA DX, QUIT
MOV AH, 09H
INT 21H
MOV AH, 01H
INT 21H
OR AL, 20H
CMP AL, 'y'
JE AGAIN
MOV AH, 04CH
INT 21H
END MAIN
Output:
cube of 5 is 125
continue? y for yes else for no
The assembly code is for finding the cube of a single digit number (0-9) using the emu8086 assembler and then displaying the result. Here is an explanation of the code, section by section:
Setup and Data Segment
assembly
.MODEL SMALL
.STACK 100H
.DATA
MSG DB 0AH, 0DH, "ENTER A NUMBER TO FIND CUBE OF IT: $"
OUT1 DB 0AH, 0DH, "CUBE OF $"
OUT2 DB " IS $"
QUIT DB 0AH, 0DH, "CONTINUE? Y FOR YES ELSE FOR NO: $"
.MODEL SMALL specifies the memory model.
.STACK 100H reserves 256 bytes for the stack.
.DATA segment defines the messages used in the program:
MSG prompts the user to enter a number.
OUT1 and OUT2 are used to display the result.
QUIT asks the user if they want to continue.
Code Segment and Main Procedure
assembly
.CODE
MAIN:
MOV AX, @DATA
MOV DS, AX
.CODE segment contains the instructions.
MAIN: is the entry point of the program.
MOV AX, @DATA and MOV DS, AX initialize the data segment register.
Main Loop and User Input
assembly
AGAIN:
LEA DX, MSG
MOV AH, 09H
INT 21H
MOV AH, 01H
INT 21H
PUSH AX
CMP AL, 39H
JG AGAIN
CMP AL, 30H
JL AGAIN
AGAIN: is a loop label for repeated execution.
LEA DX, MSG loads the address of MSG into DX.
MOV AH, 09H and INT 21H display the message to the user.
MOV AH, 01H and INT 21H read a character (the number) from the keyboard.
The input character is pushed onto the stack for later use.
The code checks if the input is between '0' (30H) and '9' (39H). If not, it loops back to AGAIN.
Cube Calculation
assembly
PUSH AX
PUSH AX
SUB AL, 30H
MOV BL, AL
MUL BL
AAM
MOV BX, AX
POP AX
SUB AL, 30H
MUL BL
AAM
MOV CL, AL
MOV CH, AH
POP AX
SUB AL, 30H
MOV BL, AL
MOV AL, BH
MUL BL
AAM
ADD AL, CH
AAA
MOV BX, AX
The input character is converted to a number by subtracting '0' (30H).
MOV BL, AL stores the number in BL.
MUL BL multiplies the number by itself (squares it).
AAM adjusts the result for ASCII arithmetic.
The result is stored in BX.
The input number is restored from the stack and multiplied again to get the cube.
The result is split into high and low parts (stored in CL and CH).
Another multiplication is performed to complete the cube calculation, and the results are adjusted and stored in BX.
Display Result
assembly
LEA DX, OUT1
MOV AH, 09H
INT 21H
POP DX
MOV AH, 02H
INT 21H
LEA DX, OUT2
MOV AH, 09H
INT 21H
MOV DL, BH
ADD DL, 30H
MOV AH, 02H
INT 21H
MOV DL, BL
ADD DL, 30H
MOV AH, 02H
INT 21H
MOV DL, CL
ADD DL, 30H
MOV AH, 02H
INT 21H
LEA DX, OUT1 loads the address of OUT1 into DX.
MOV AH, 09H and INT 21H display the "CUBE OF " message.
POP DX retrieves the original number from the stack and displays it.
LEA DX, OUT2 displays the " IS " message.
The result stored in BX and CL is adjusted to ASCII by adding '0' and displayed using INT 21H.
Continue or Exit
assembly
LEA DX, QUIT
MOV AH, 09H
INT 21H
MOV AH, 01H
INT 21H
OR AL, 20H
CMP AL, 'y'
JE AGAIN
MOV AH, 04CH
INT 21H
END MAIN
LEA DX, QUIT loads the address of the quit message.
MOV AH, 09H and INT 21H display the quit message.
MOV AH, 01H and INT 21H read the user's input.
If the user inputs 'y' (or 'Y'), it loops back to AGAIN.
Otherwise, it exits the program using MOV AH, 04CH and INT 21H.
Summary
The code repeatedly prompts the user to enter a number, calculates its cube, displays the result, and asks if the user wants to continue. The input is validated to ensure it's a single digit, and the cube calculation involves basic arithmetic and ASCII adjustment to display the result correctly.
in clear:
Data Segment:
.MODEL SMALL
: This directive specifies that the program uses the small memory model, meaning it can access up to 64KB of data..STACK 100H
: This allocates 100h (256 bytes) for the program's stack.- Data definitions:
MSG
: This defines a string containing a message prompting the user to enter a number.OUT1
,OUT2
,QUIT
: These define strings used to display output messages.
Code Segment:
MAIN
label marks the beginning of the program's main function.MOV AX, @DATA
: This instruction loads the segment address of the data segment into the AX register.MOV DS, AX
: This instruction sets the Data Segment register (DS) to the address loaded in AX, making the data segment accessible for data transfers.
Input Loop (AGAIN
label):
LEA DX, MSG
: This instruction loads the address of theMSG
string into the DX register using the Lea (Load Effective Address) instruction.MOV AH, 09H
: This sets the AH register to 09h, which is the interrupt function number for displaying a string on the screen.INT 21H
: This triggers an interrupt 21h service call, which calls the operating system function to display the string pointed to by DX.MOV AH, 01H
: This sets AH to 01h, the interrupt for getting a character from the keyboard.INT 21H
: This calls another interrupt 21h service to read a character from the keyboard and store it in the AL register.PUSH AX
: This pushes the entered character (in AL) onto the stack to preserve it.
Input Validation:
CMP AL, 39H
: This compares the character in AL with '9' (ASCII code 39h).JG AGAIN
: If the character is greater than '9' (i.e., not a valid digit), it jumps back to theAGAIN
label to prompt for input again.CMP AL, 30H
: This compares the character with '0' (ASCII code 30h).JL AGAIN
: If the character is less than '0', it also jumps back toAGAIN
for invalid input.
Calculating the Cube:
PUSH AX
: This pushes a copy of the character onto the stack again.PUSH AX
: This pushes another copy of the character onto the stack, for a total of three copies (needed for calculating the cube).
Next, the code performs the cube calculation iteratively, multiplying the entered digit three times with itself.
SUB AL, 30H
: This subtracts '0' (30h) from the character in AL to convert it to a numerical value.MOV BL, AL
: This moves the converted digit to BL register.MUL BL
: This multiplies the digit in AL with the copy in BL, resulting in the digit squared and stored in AX.AAM
: This instruction performs ASCII Adjust for Multiplication, converting the binary result in AX to a two-digit decimal format (useful for displaying the result later).MOV BX, AX
: This moves the two-digit decimal result (in AX) to BX, with the higher digit stored in BH and the lower digit in BL.
This process is repeated twice more, multiplying the squared value with the original digit again.
POP AX
: This pops the second copy of the digit from the stack.SUB AL, 30H
: Converts the digit in AL to a numerical value.MOV BL, AL
: Moves the converted digit to BL.MOV AL, BH
: Moves the higher digit of the previously calculated squared value (from BX) to AL. This is because we want to multiply the squared value (tens digit) with the original digit (units digit).MUL BL
: Multiplies the higher digit (in AL) with the digit in BL, resulting in a product that contributes to the hundreds digit of the cube.AAM
: Converts the binary result in AX to a two-digit decimal format.
Here, we need to add the carry flag (CF) set during the previous multiplication to the current result in AL. This is because the product might have resulted in a carry that needs to be considered for the final cube value.
0 Comments