section .data msg1 db 0xa, 0xb, "enter a Digit (0-9): " ; Message asking for input msg2 db 0xa, 0xb, "squared number is: " ; Message to show squared result msg3 db 0xa, 0xb, "Enter between 0 - 9 " ; Error message if input is out of range section .bss inbuf resb 1 ; Buffer for storing single character input outbuf resb 2 ; Buffer for storing 2 characters output (square result) section .text global _start _start: ; Print the input prompt message mov edx, 23 ; Length of msg1 mov ecx, msg1 ; Point to msg1 mov ebx, 1 ; File descriptor (1 = stdout) mov eax, 4 ; sys_write system call int 0x80 ; Call kernel ; Read a single character from stdin mov ebx, 0 ; File descriptor (0 = stdin) mov ecx, inbuf ; Buffer to store the input mov edx, 1 ; Number of bytes to read mov eax, 3 ; sys_read system call int 0x80 ; Call kernel ; Validate if input is between '0' and '9' mov al, byte[inbuf] ; Load input character into AL cmp al, '0' ; Check if input is less than '0' jl ERRORMSG ; Jump to error if less than '0' cmp al, '9' ; Check if input is greater than '9' jg ERRORMSG ; Jump to error if greater than '9' ; Print the entered value mov edx, 1 ; Length of inbuf mov ecx, inbuf ; Point to inbuf mov ebx, 1 ; File descriptor (stdout) mov eax, 4 ; sys_write int 0x80 ; Call kernel to print input character ; Convert ASCII character to numerical value mov al, byte[inbuf] ; Load character again sub al, '0' ; Convert ASCII to integer (0-9) ; Square the number (AL * AL) mul al ; Multiply AL by itself, result is in AX (AL * AL) ; Split result into tens and units mov bl, 10 ; Divisor for decimal conversion div bl ; Divide AX by 10: AL = quotient, AH = remainder add al, '0' ; Convert quotient to ASCII add ah, '0' ; Convert remainder to ASCII mov word[outbuf], ax ; Store the two ASCII characters in outbuf ; Print the output message for the squared number mov edx, 21 ; Length of msg2 mov ecx, msg2 ; Point to msg2 mov ebx, 1 ; File descriptor (stdout) mov eax, 4 ; sys_write int 0x80 ; Call kernel ; Print the squared number mov edx, 2 ; Length of outbuf mov ecx, outbuf ; Point to outbuf mov ebx, 1 ; File descriptor (stdout) mov eax, 4 ; sys_write int 0x80 ; Call kernel ; Exit the program mov eax, 1 ; sys_exit mov ebx, 0 ; Exit code 0 int 0x80 ; Call kernel ERRORMSG: ; Display error message if input is not in range mov edx, 21 ; Length of msg3 mov ecx, msg3 ; Point to msg3 mov ebx, 1 ; File descriptor (stdout) mov eax, 4 ; sys_write int 0x80 ; Call kernel jmp ENDPROG ; Jump to program end ENDPROG: ; Exit program mov eax, 1 ; sys_exit mov ebx, 5 ; Exit code 5 (error) int 0x80 ; Call kernel
To embed this project on your website, copy the following code and paste it into your website's HTML: