不甘的甘愿 2024-03-28 18:08 采纳率: 0%
浏览 12
已结题

汇编语言除法溢出问题

使用汇编语言编写程序,从键盘输入设x为16个字节的A,而除数为4个字节的B,计算A/B,商放在RESULT中,余数放在EXTRA中。
要求:1、要有判断除数不为零的过程
2、考虑除法溢出问题(商太大,寄存器溢出)
3、思考A和B是有符号数还是无符号数
4、环境为MASM
5、注意A和B是从键盘输入
6、用以下格式
DATAS SEGMENT
;此处输入数据段代码
DATAS ENDS

STACKS SEGMENT
;此处输入堆栈段代码
STACKS ENDS

CODES SEGMENT
ASSUME CS:CODES,DS:DATAS,SS:STACKS
START:
MOV AX,DATAS
MOV DS,AX
;此处输入代码段代码
MOV AH,4CH
INT 21H
CODES ENDS
END START

  • 写回答

18条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-03-28 18:09
    关注

    引自免费微信小程序:皆我百晓生

    在MASM汇编环境下,我们可以编写一个处理上述需求的程序,这里假设A和B都是有符号的32位整数(因为A是16个字节,即4个双字DWORDs,B是4个字节),并且使用int 21h服务调用来获取键盘输入以及显示结果。以下是一个简化的示例代码片段:

    .386
    .model flat, stdcall
    .stack 4096
    ExitProcess PROTO, dwExitCode:DWORD
    
    .data
    prompt1 db 'Enter the dividend (4 DWORDs): $'
    prompt2 db 'Enter the divisor (1 DWORD): $'
    resultPrompt db 'The quotient is: $'
    extraPrompt db 'The remainder is: $'
    
    dividend DWORD   ? dup(?)
    divisor  DWORD   ?
    quotient DWORD   ?
    remainder DWORD ?
    
    .code
    main PROC
        ; Prompt user to enter dividend
        mov edx, OFFSET prompt1
        call WriteString
        mov ecx, SIZEOF dividend
        lea eax, dividend
        call ReadDWords        ; Custom function to read 4 DWORDs from keyboard
    
        ; Prompt user to enter divisor
        mov edx, OFFSET prompt2
        call WriteString
        call ReadDWord         ; Custom function to read 1 DWORD from keyboard into divisor
    
        ; Check if divisor is zero
        cmp divisor, 0
        je DivisionByZeroError
    
        ; Initialize registers for division
        mov esi, OFFSET dividend  ; ESI points to the first DWORD of dividend
        xor edi, edi              ; EDI will hold the quotient
        xor ebx, ebx              ; EBX will hold the remainder
    
    DivideLoop:
        ; Divide the current DWORD by the divisor
        mov eax, [esi]
        cdq                       ; Sign extend EAX into EDX:EAX
        idiv divisor              ; Signed divide, quotient -> EAX, remainder -> EDX
        
        ; Store the quotient and update the remainder
        mov [edi], eax
        add edi, TYPE DWORD       ; Move to next DWORD in quotient
        mov ebx, edx              ; Update the remainder
    
        ; Move to the next DWORD in dividend
        add esi, TYPE DWORD
        cmp esi, OFFSET dividend + SIZEOF dividend
        jl DivideLoop            ; Continue until all DWORDs are processed
    
        ; At this point, EBX holds the final remainder
        ; And the quotient is stored in the RESULT array
    
        ; Display the result
        mov edx, OFFSET resultPrompt
        call WriteString
        lea eax, quotient
        mov ecx, SIZEOF quotient
        call WriteDWords          ; Custom function to write quotient to console
    
        mov edx, OFFSET extraPrompt
        call WriteString
        mov eax, ebx
        call WriteInt             ; Custom function to write remainder to console
    
        invoke ExitProcess, 0
    main ENDP
    
    DivisionByZeroError:
        ; Handle division by zero error here
        ; You can display an error message and exit or handle it as desired
        jmp ExitProcess, 1
    
    ; Custom functions prototypes (not shown - you need to implement these):
    ; WriteString proto uses edx
    ; ReadDWords proto uses ecx, eax
    ; ReadDWord proto uses eax
    ; WriteDWords proto uses ecx, eax
    ; WriteInt proto uses eax
    
    END main
    

    由于MASM汇编没有内建的多字节整数除法指令,我们通过循环逐个处理每个DWORD,并使用sarshr等指令扩展处理更大的数据类型。同时,idiv指令会自动处理溢出问题,当发生溢出时,CPU标志寄存器中的OF(Overflow Flag)会被置位。在这个示例中,我们没有检查溢出,但实际项目中可以添加相关检查。

    注意,以上代码未包含读取和写入字符串及数值到控制台的自定义函数实现,你需要自行补充这部分内容。此外,这个示例假定用户输入的是合法的32位有符号整数,实际情况中应加入相应的错误检查和处理机制。

    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月29日
  • 赞助了问题酬金15元 3月28日
  • 修改了问题 3月28日
  • 修改了问题 3月28日
  • 展开全部

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来