解决装配中的分流溢出 [英] Solving Divide Overflow in Assembly

查看:84
本文介绍了解决装配中的分流溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我正在做一项作业,输入输入后一直遇到除数溢出.在下面,我已经附加了我应该做的事情(在代码顶部),并且已经附加了我的代码.

Hello

I am working on an assignment, and I`ve been encountering divide overflow after entering my input. Below, I`ve attached what I am supposed to do (on the top of my code), and I`ve attached my code.

; Number Conversion Program
;
; Requirement #1: Your program should clearly prompt the user with a message to enter an
exactly 4 digit decimal number, which you should store as a nullterminated string (i.e., 5 characters total).
Requirement #2: If the user types fewer than 4 digits before pressing return, then your
program should ask for a 4 digit number again.  Otherwise, your program
should efficiently convert the 4 character numeric string into a 16-bit
numeric value and store this in a word variable.
Requirement #3: Using a loop, convert the 16-bit value into a 16 character string of ‘0’s and
‘1’s.  Store this in another null-terminated string.

 org 100h

section .text

start:
        mov bx, input           ;get address of first char for input
        mov cx, 2

myloop:
        mov ah, 00h             ;service 00h (get keystroke)
        int 16h                         ;call interrupt 16h (AH=00H) and character read will now be in AL
        cmp al, 0Dh             ;check if user pressed enter
        je convert                      ;if so, convert
        mov [bx], ax            ;if not, store character
        inc bx                          ;point BX to next char of input
        jmp myloop                      ;repeat for next character

convert:
        div cx                          ;dividing the number by 2 since binary is base 2
        add dx, 30h
        cmp ax, 0
        je output
        mov [bx], al
        inc bx
        jmp convert

output:
    mov dl, [bx]    ; get char at address in BX
    inc bx          ; point BX to next char in message
    cmp dl, 0       ; Is DL = null (that is, 0)?
    je  quit        ; If yes, then quit
    mov ah, 06      ; If no, then call int 21h service 06h
    int 21h         ;    to print the character
    jmp myloop      ; Repeat for the next character

quit:
        int 20h                         ;quit program

section .data
        input db 0, 0, 0, 0, 0

推荐答案

为什么要使用DIV除以2?为什么不将数字右移一位呢? /a> [^ ]-并忽略了不需要一分为二...

右移一位除以2,左移一位乘以2.

但是您对家庭作业的描述和您向我们展示的代码与我所看到的完全不匹配!

如果要从字符串转换为二进制数,则最明显的方法是从最高有效(最左)到最低有效(最右)的ASCII数字循环,将每个数字转换为数字(按减去30h),将当前总数乘以10,然后加上数字.

由于这是您的家庭作业,因此我不会给您任何代码.
Why the heck are you using DIV to divide by two? Why not shift the number one place to the right instead: http://en.wikibooks.org/wiki/X86_Assembly/Shift_and_Rotate[^] - and that''s ignoring that you don''t need to divide by two in the first place...

A single bit shift right is a divide by 2, and single shift left is a multiply by two.

But the description of your homework and the code you show us do not match in any way that I can see!

If you are converting from a string to a binary number, then the most obvious way is to loop through the ASCII digits from the most significant (left-most) to the least significant (right-most), converting each to a number (by subtracting 30h), multiply the current total by ten, and adding the digit number.

Since this is your homework, I''ll give you no code.


这篇关于解决装配中的分流溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆