分数转换至小数点NASM [英] Converting fractions to decimals NASM

查看:130
本文介绍了分数转换至小数点NASM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有固定了很多,因为我的最后一个职位,但我仍然没有得到结果。我们正在努力在8086微处理器和NASM汇编程序。我的code的伟大工程的权利,直到它即将给出的结果。它会显示第三信息的个数为:并打印小数点。但它不会打印后的任何数字,我不能键入或退出程序或任何东西。我不得不关闭DOSBox中并重新运行。请帮助。

 组织100H
;程序转换分数,M / N,为十进制,其中M< N,M&安培; n均为正,且打印高达6位小数
段.data
MSG1 DB输入分子,$
MSG2 DB输入分母,$
MSG3 DB的数目是:,$
EMSG DB请输入0和9之间的数字,$
.bss段
中号RESB 1
ñRESB 1.text段
主要:
;打印用户提示
MOV DX,MSG1;得到消息
MOV啊,09H;显示字符串函数
INT 21H;显示它
调用DEC_IN
MOV [M],BX;移动到分子存储单元M
;打印第二个提示
MOV DL,0AH;换行移入字符显示寄存器
MOV啊,02H;字符内显示功能
INT 21H;显示换行
MOV DL,0DH;回车移入字符显示寄存器
INT 21H;显示回车
MOV DX,MSG 2;得到消息
MOV啊,09H;显示字符串函数
INT 21H;显示它
调用DEC_IN
MOV [N],BX;店内分母在内存地址n
MOV DL,0AH;换行移入字符显示寄存器
MOV啊,02H;字符内显示功能
INT 21H;显示换行
MOV DL,0DH;回车移入字符显示寄存器
INT 21H;显示回车
MOV DX,MSG3;得到消息
MOV啊,09H;显示字符串函数
INT 21H;显示它
MOV DL,2Eh中断;移动'。显示字符寄存器
MOV啊,02H;显示字符功能
INT 21H;它显示
MOV CX,6;设置循环运行6倍
MOV BX,[M]。在并购prepare分子要乘以
JMP打印
DEC_IN:
;从键盘输入字符,转换为ASCII码相应的二进制
推斧
异或BX,BX
。最佳:
MOV啊,01H;键盘输入功能
INT 21H;字符输入,复制字符到人
CMP人,0DH;是输入回车?
JE .done;用户完成
CMP人,30H;比较输入到ASCII code为0
JB错误;如果输入是小于0跳转到错误
CMP人,39H;比较输入到ASCII code 9
JA错误;如果输入大于9跳转到错误
子人,30H;减去30小时,使ASCII code转化基地10号
IMUL BX,10;万一的数目是一个以上的位
MOV啊,0;副本之前清楚啊
加BX,AX;店内AX在BX,因此它可以再次运行。
JMP .TOP
.done:
流行斧
RET打印:
;循环打印
MOV人,10; prepare人乘法
MUL BX;乘以10的分子,导致AX
MOV BX,[N];移动分母BX是除数
DIV BX;除以分母商AX在AL在AH余
加人,30H;转换商各自的ASCII符号
MOV DL,人;移动商到显示字符寄存器
推斧;保存在AH其余由堆栈推AX
MOV啊,02H;显示字符功能
INT 21H;显示它
流行斧;从堆栈中弹出AX检索AH余
MOV人,0;清除商,AL所以只有剩下的啊,在AX
MOV BX,AX;移动剩余部分BX,因此它可以再次运行
环打印
JMP出口错误:
;然后显示错误消息跳回DEC_IN
MOV DL,0AH;换行移入字符显示寄存器
MOV啊,02H;字符内显示功能
INT 21H;显示换行
MOV DL,0DH;回车移入字符显示寄存器
INT 21H;显示回车
MOV DX,EMSG;移动错误信息到显示字符串寄存器
MOV啊,09H;显示字符串函数
INT 21H;它显示
MOV DL,0AH;换行移入字符显示寄存器
MOV啊,02H;字符内显示功能
INT 21H;显示换行
MOV DL,0DH;回车移入字符显示寄存器
INT 21H;显示回车
JMP主出口:
;退出到DOS
 MOV啊,04CH; DOS功能:退出程序
 MOV人,0;返回退出code值
 INT 21H;呼叫DOS。终止程序


解决方案

确定我固定它。这些16位寄存器有猫腻,所以我使用的乘法和除法的8位寄存器。我不得不解决的唯一的事情是例行打印的code的其余部分是相同的,只是在主:跳到印刷前的最后一个指令:改为 MOV BL,[M] 。这里是纠正日常打印

 打印:
;循环打印
异斧,斧;明确斧
MOV人,10; prepare乘法
MUL BL;由10 AL乘以BL分子
XOR BL,BL;明确BL被再次使用
MOV BL,[N];中移动BL分母
DIV BL;在BL分母AX分裂分子* 10
MOV DL,人;移动商在AL显示字符寄存器
推斧;保存在AH余
添加DL,30H;转换商各自的ASCII符号
异斧,斧;清晰的斧头功能
MOV啊,02H;显示字符功能
INT 21H;显示它
流行斧;在AH剩余检索
MOV BL啊;移动剩余部分BL开始遍历
环打印
JMP出口

So I have fixed a lot since my last post but I am still not getting the result. We are working on the 8086 microprocessor and NASM assembler. My code works great right up until it is about to give the result. It will display the 3rd message "The number is: " and prints the decimal point, "." but it won't print any numbers after that and I can't type or exit the program or anything. I have to close DOSBox and run it again. Please help.

       org 100h
; program converts fraction, M/N, to decimal, where M < N, M & N are both positive, and upto 6 decimal places are printed
section .data
MSG1    db  "Enter the numerator: ", '$'
MSG2    db  "Enter the denominator: ", '$'
MSG3    db  "The number is: ", '$'
EMSG    db  "Please enter a number between 0 and 9 ", '$'
section .bss
M   RESb    1   
N   RESb    1

section .text
main:
; print user prompt 
mov     dx, MSG1    ; get message
mov     ah, 09h     ; display string function
int     21h         ; display it
call    DEC_IN      
mov     [M], bx     ; move numerator to memory location M
; print second prompt
mov     dl, 0Ah     ; line feed moved into character display register
mov     ah, 02h     ; charcter display function
int     21h         ; display line feed
mov     dl, 0Dh     ; carriage return moved into character display register
int     21h         ; display carriage return
mov     dx, MSG2    ; get message
mov     ah, 09h     ; display string function
int     21h         ; display it
call    DEC_IN
mov     [N], bx     ; store denominator in memory location N
mov     dl, 0Ah     ; line feed moved into character display register
mov     ah, 02h     ; charcter display function
int     21h         ; display line feed
mov     dl, 0Dh     ; carriage return moved into character display register
int     21h         ; display carriage return
mov     dx, MSG3    ; get message
mov     ah, 09h     ; display string function
int     21h         ; display it
mov     dl, 2Eh     ; moves '.' to display character register
mov     ah, 02h     ; display character function
int     21h         ; displays it
mov     cx, 6       ; set loop to run 6 times
mov     bx, [M]     ; prepare numerator in M to be multiplied
jmp     print
DEC_IN:
; input character from keyboard, converts ASCII to appropriate binary
push    ax
xor     bx,bx
.top:
mov     ah, 01h     ; keyboard input function
int     21h         ; character input, copies character into al
cmp     al, 0Dh     ; is the input a carriage return?
je      .done       ; user is done
cmp     al, 30h     ; compares input to ASCII code for 0
jb      error       ; if input is less than 0 jump to error
cmp     al, 39h     ; compares input to ASCII code for 9
ja      error       ; if input is greater than 9 jump to error
sub     al, 30h     ; subtracts 30h to make the ASCII code into the base 10 number
imul    bx, 10      ; in case the number is more than one digit
mov     ah, 0       ; clear ah before copy
add     bx, ax      ; store ax in bx so it can run again.
jmp     .top
.done:
pop     ax
ret

print:
; loop to print 
mov     al, 10      ; prepare al for multiplication
mul     bx          ; multiply numerator by 10, result in AX
mov     bx, [N]     ; move denominator to bx to be divisor
div     bx          ; divide AX by denominator quotient in AL remainder in AH
add     al, 30h     ; convert quotient to respective ASCII symbol
mov     dl, al      ; move quotient into display char register
push    ax          ; save the remainder in AH by pushing AX on stack
mov     ah, 02h     ; display character function
int     21h         ; display it    
pop     ax          ; retrieve remainder in AH by popping AX from stack
mov     al, 0       ; clear the quotient, AL so only the remainder, AH, is in AX 
mov     bx, ax      ; move remainder to bx so it can run again
loop    print
jmp     exit

error:
; displays error message then jumps back to DEC_IN
mov     dl, 0Ah     ; line feed moved into character display register
mov     ah, 02h     ; charcter display function
int     21h         ; display line feed
mov     dl, 0Dh     ; carriage return moved into character display register
int     21h         ; display carriage return
mov     dx, EMSG    ; moves error message into display string register
mov     ah, 09h     ; display string function
int     21h         ; displays it
mov     dl, 0Ah     ; line feed moved into character display register
mov     ah, 02h     ; charcter display function
int     21h         ; display line feed
mov     dl, 0Dh     ; carriage return moved into character display register
int     21h         ; display carriage return
jmp     main        

exit:
;exit to DOS
 mov     ah, 04Ch      ; DOS function: Exit program 
 mov     al, 0         ; Return exit code value
 int     21h           ; Call DOS. Terminate program 

解决方案

ok I fixed it. Those 16 bit registers are tricky so I used 8 bit registers for the multiplication and division. The only thing I had to fix was routine print: the rest of the code is the same, except that in main: the last instruction before the jump to print: was changed to mov bl,[m]. here is the corrected routine print::

print:
; loop to print 
xor     ax, ax      ; clear ax
mov     al, 10      ; prepare for multiplication
mul     bl          ; multiply numerator in BL by 10 in AL
xor     bl, bl      ; clear bl to be used again
mov     bl, [N]     ; move denominator in to BL
div     bl          ; divide numerator*10 in AX by denominator in BL
mov     dl, al      ; move quotient in AL to display char register
push    ax          ; save the remainder in AH
add     dl, 30h     ; convert quotient to respective ASCII symbol
xor     ax, ax      ; clear ax for function
mov     ah, 02h     ; display char function
int     21h         ; display it    
pop     ax          ; retrieve remainder in AH
mov     bl, ah      ; move remainder to BL to start loop over
loop    print
jmp     exit

这篇关于分数转换至小数点NASM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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