Two-Digit Sum and Square - Assembly Program
source code
Explanation:
This assembly program takes a two-digit number as input from the user, calculates the sum of its digits, and then computes and displays the square of the sum.
Here's how the program works step by step:
The program starts with the
_start
label, which serves as the entry point for the program.The
inputmessage
subroutine is called to display a message asking the user to enter a two-digit number. It uses themsg
string and prints it using theint 80h
system call.The
input
subroutine is called to read the user input. It reads two characters (digits) using theint 0x80
system call and stores the input in theinp
buffer.The
convert
subroutine converts the ASCII characters of the two digits into an actual numeric value. It subtracts30h
(ASCII value of '0') from the ASCII values of the digits and then multiplies and adds them to form the numeric value.The
sum
loop calculates the sum of the two digits by adding them together repeatedly using theadd
instruction andloop
control structure.The
breakdigits
loop is responsible for breaking down the sum into individual digits and pushing them onto the stack. It uses integer division and thediv
instruction to separate the digits.The program then calls the
outputmessage
subroutine to display the message "SQUARE = ".The
sprint
loop retrieves the digits from the stack, one by one, and prints them using theprint
subroutine.Finally, the program calls the
printnewline
subroutine to print a newline character and then exits using theint 0x80
system call.
Example:
Let's run through an example of how the program works:
Suppose the user enters the two-digit number "25" as input.
The program will display the message "ENTER THE NUMBER (2 digit)".
The user inputs "25".
The
convert
subroutine converts the ASCII values of '2' and '5' into the numeric values 2 and 5, respectively.The
sum
loop calculates the sum of 2 and 5, resulting in 7.The
breakdigits
loop breaks down 7 into individual digits and pushes them onto the stack: '7'.The
outputmessage
subroutine prints "SQUARE = ".The
sprint
loop pops '7' from the stack and prints it.The program prints a newline character.
The program exits.
Output:
This program demonstrates basic input and output handling in x86 assembly language, along with numeric conversions and arithmetic operations. The program is designed to work with two-digit numbers, and its behavior is based on the user's input.
Make sure to assemble and run the code on an appropriate x86 assembly environment to observe the actual output.
Last updated