本文介绍了如何在 Tasm 中将字符串转换为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写了一个读取字符串并将其转换为数字的程序.以十进制系统输入的数字的字符串表示形式.转换的结果在寄存器 ax 中.我用的是 tasm16.
I made a program that reads a string and converts it into a number. A string representation of the number entered in the decimal system. The result of of converting is in register ax.I use tasm16.
如何转换更大的字符串(大于 65535)?
How do I do that I can convert a string bigger(more than 65535)?
model small
stack 100h
printstring macro msg
mov ah,09h
mov dx,offset msg
int 21h
endm
data segment
cr equ 0dh
lf equ 0ah
errorInput db 'Wrong input',cr,lf,'$'
inputAmsg db 'Please input A',cr,lf,'$'
outstring db 'Output:',cr,lf,'$'
maxAbuf db 10
db ?
db 10 dup(0)
db '$'
result dw 0
data ends
code segment
assume cs:code, ds:data
start:
mov ax,data
mov ds,ax
;input A
printstring inputAmsg
mov ah,0ah
lea dx,maxAbuf
int 21h
printstring outstring
mov ah,09h
lea dx,maxAbuf+2
int 21h
;in:
;dx:address of string
;out
;dx:ax - result ;work only ax
;
call Convert
jmp exit
;in:
;dx:address of string
;out
;dx:ax - result
;9999989d = 989675h
Convert proc
xor ax,ax
xor cx,cx
mov bx,10 ;base
mov si, offset dx
xor dx,dx
getnewchar: mov cl,[si]
cmp cl,0dh ;if `$` exit
jz endproc
sub cl,'0' ;or 30h
mul bx
add ax,cx
;I do not know what to write here, if there is an exit for the bit grid.
adc dx,0
inc si
jmp getnewchar
endproc:
ret
endp
exit: mov ax,4c00h
int 21h
code ends
end start
推荐答案
例如,您可以像这样将乘法从 16 位扩展 10 到 32 位:
You could, for example, extend your multiplication by 10 from 16 to 32 bits like this:
; file: mul32ten.asm
; compile: nasm.exe mul32ten.asm -f bin -o mul32ten.com
bits 16
org 0x100
xor dx, dx
xor ax, ax
mov si, MyStr
NextDigit:
mov bl, [si]
or bl, bl
je Done
call MulDxAxBy10
sub bl, '0'
add al, bl
adc ah, 0
adc dx, 0
inc si
jmp NextDigit
Done:
; dx:ax should be equal to 7FFFFFFF now
ret
MulDxAxBy10:
push si
push di
shld dx, ax, 1
shl ax, 1 ; dx:ax = n * 2
mov si, ax
mov di, dx ; di:si = n * 2
shld dx, ax, 2
shl ax, 2 ; dx:ax = n * 8
add ax, si
adc dx, di ; dx:ax = n * 8 + n * 2 = n * (8 + 2) = n * 10
pop di
pop si
ret
MyStr db "2147483647", 0
这篇关于如何在 Tasm 中将字符串转换为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!