汇编,有符号十进制数输入输出

STA SEGMENT STACK
    DW 40H DUP(?)
    TOP LABEL WORD
STA ENDS
DATA SEGMENT


  CRLF DB 0DH,0AH,24H
DATA ENDS


CODE SEGMENT


   ASSUME CS:CODE,SS:STA,DS:DATA
   
  MAIN PROC FAR
   MOV AX,STA
   MOV SS,AX
   LEA SP,TOP
   MOV AX,DATA
   MOV DS,AX


   MOV CX,5
   XOR BX,BX
   JMP FI


FU: MOV DX,1
    PUSH DX
    JMP L1
FN:
    DEC CX
    JMP FNN
FUO:
    NEG BX
    JMP L5
       
FI: MOV AH,1
    INT 21H
    CMP AL,0DH
    JE L3
    CMP AL,'+'
    JE L1
    CMP AL,2DH
    JE FU
   CMP AL,30H
   JAE FN
  
    
   
L1:MOV AH,1
   INT 21H
   
   CMP AL,0DH
   JE L3
   CMP AL,30H
   JB L1
 
   CMP AL,39H
   JA L1
FNN:
   AND AX,0FH                    ;0000 0000 0000 1111
   XCHG AX,BX                   ;交换两个数
   MOV SI,10
   MUL SI
   JC L2                              ;判断溢出,若溢出,则退出
   ADD BX,AX
   JC L2
   LOOP L1                           ;这里规定最多输五位的十进制数


L3:


   MOV AH,9
   LEA DX,CRLF                    ;回车换行
   INT 21H


   
   MOV CX,16
   POP DX
   CMP DX,1
   JNE L5
   NEG BX
   
   
L5: XOR DX,DX
   SHL BX,1
   ADC DL,0
   AND DL,1
   OR DL,30H
   
   MOV AH,2
   INT 21H
   LOOP L5


L2:MOV AH,4CH
   INT 21H
   MAIN ENDP
   CODE ENDS
   END MAIN
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是用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 ``` 运行程序后,输入一个无符号十进制,然后程序会将该输出。如果输入的不是有效的十进制,则程序会提示错误信息并要求重新输入
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值