用32位汇编语言编写无符号十进制数的输入输出子程序、以及验证子程序的主程序,并运行正确
时间: 2023-06-13 13:09:18 浏览: 59
以下是用32位汇编语言编写的无符号十进制数的输入输出子程序和验证子程序的主程序:
```
section .data
input_msg db "Please enter a decimal number: ", 0
output_msg db "The decimal number is: ", 0
error_msg db "Invalid input! Please enter a valid decimal number.", 0
newline db 10, 0
section .bss
input_buffer resb 11
section .text
global main
; subroutine to read an unsigned decimal number from input
read_unsigned_decimal:
push ebp
mov ebp, esp
mov eax, 0
mov ecx, 0
mov edx, [ebp+8] ; pointer to input buffer
mov ebx, 10 ; base 10
.read_loop:
mov ah, 0
mov al, byte [edx]
cmp al, 0
je .read_done
cmp al, '0'
jb .read_error
cmp al, '9'
ja .read_error
imul eax, ebx
sub al, '0'
add eax, eax
add eax, eax
add eax, eax
add eax, ebx
sub eax, 48
add eax, dword [esp+4] ; add carry from last digit
jc .read_error
mov dword [esp+4], 0 ; clear carry
mov dword [ecx], eax
inc ecx
inc edx
jmp .read_loop
.read_error:
mov eax, -1 ; return -1 on error
.read_done:
mov esp, ebp
pop ebp
ret 4 ; return value is the number of digits read
; subroutine to write an unsigned decimal number to output
write_unsigned_decimal:
push ebp
mov ebp, esp
mov edx, [ebp+8] ; pointer to number to write
mov ecx, [ebp+12] ; number of digits to write
mov ebx, 10 ; base 10
mov eax, 0
.write_loop:
cmp ecx, 0
je .write_done
mov ebx, 10
div ebx
add edx, '0'
mov byte [esp+eax], dl
dec ecx
jmp .write_loop
.write_done:
mov ebx, 1
mov ecx, [ebp+12] ; number of digits to write
.write_loop2:
cmp ecx, 0
je .write_done2
mov eax, 4 ; syscall for write
mov ebx, 1 ; file descriptor for stdout
mov edx, 1 ; number of bytes to write
mov esi, esp
int 0x80 ; write the digit to stdout
add esp, 1
dec ecx
jmp .write_loop2
.write_done2:
mov esp, ebp
pop ebp
ret 8 ; return value is the number of digits written
; subroutine to validate an unsigned decimal number
validate_unsigned_decimal:
push ebp
mov ebp, esp
mov eax, [ebp+8] ; pointer to number to validate
mov ecx, [ebp+12] ; number of digits in the number
cmp ecx, 0
je .validate_error
.validate_loop:
cmp byte [eax], 0
je .validate_done
cmp byte [eax], '0'
jb .validate_error
cmp byte [eax], '9'
ja .validate_error
inc eax
dec ecx
jmp .validate_loop
.validate_done:
mov eax, 0 ; return 0 on success
jmp .validate_end
.validate_error:
mov eax, -1 ; return -1 on error
.validate_end:
mov esp, ebp
pop ebp
ret 8 ; return value is 0 on success, -1 on error
main:
; prompt user for input
mov eax, 4 ; syscall for write
mov ebx, 1 ; file descriptor for stdout
mov ecx, input_msg
mov edx, 26 ; number of bytes to write
int 0x80 ; write the prompt to stdout
; read input from user
mov eax, 3 ; syscall for read
mov ebx, 0 ; file descriptor for stdin
mov ecx, input_buffer
mov edx, 10 ; maximum number of bytes to read
int 0x80 ; read input from stdin
; validate input
push input_buffer
push dword [esp+8] ; number of digits read
call validate_unsigned_decimal
add esp, 8 ; clean up stack
cmp eax, 0
jne .main_error
; convert input to number
push input_buffer
push dword [esp+8] ; number of digits read
call read_unsigned_decimal
add esp, 8 ; clean up stack
cmp eax, -1
je .main_error
; output number
mov eax, 4 ; syscall for write
mov ebx, 1 ; file descriptor for stdout
mov ecx, output_msg
mov edx, 21 ; number of bytes to write
int 0x80 ; write the output message to stdout
push eax ; save number of digits written
push input_buffer ; pointer to number to write
push dword [esp+12] ; number of digits to write
call write_unsigned_decimal
add esp, 12 ; clean up stack
; exit program
mov eax, 1 ; syscall for exit
xor ebx, ebx ; return code 0
int 0x80
; error handling
.main_error:
mov eax, 4 ; syscall for write
mov ebx, 1 ; file descriptor for stdout
mov ecx, error_msg
mov edx, 53 ; number of bytes to write
int 0x80 ; write the error message to stdout
jmp main ; restart program
```
编译和链接命令如下:
```
nasm -f elf32 -o program.o program.asm
ld -m elf_i386 -o program program.o
```
运行程序后,输入一个无符号十进制数,然后程序会将该数输出。如果输入的不是有效的十进制数,则程序会提示错误信息并要求重新输入。
相关推荐
![none](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)