汇编语言编程中字符串到整数的转换 8086

String to Integer conversion in assembly language programing 8086

我正在尝试将字符串转换为整数,然后再转换回字符串。 但它给出了一些奇怪的值,我已经更新并尝试其他方法,请再次检查代码。为什么输出不是仪式?更多更新,但输入仍然不正确

.model small
.stack 100h
.data
input db 30 dup ('$')
n dw ?
count db ?
output db 30 dup ('$')
.code
main proc

mov ax , @data
mov ds , ax 
mov bx , offset input
mov count , 0

; mov 斧头, 0

l1:
    mov ah , 1
    int 21h
    mov [bx] , al
    inc bx 
    inc count
    cmp al , 13
    jne l1
    mov ax , 0  

    mov bx , offset input

l2 : 
dec bx
mov dx , [bx]
add dx , 48
mov cx , 10 
mul cx 
add ax , [bx]

dec count 
cmp count , 0
jne l2

    mov ax , bp

    mov bx , offset output

l3 : ;  loop
    mov dx, 0 ; dx = 0
    mov cx,10  ; cx is a loop variable      
    div cx
    add dl,48 ; adding 48 for ascii character
    mov [bx], dl ; adding the value of  each number in array
    inc bx ; decreament pointer 
    cmp ax, 0 ; comparing ax 
    jne l3 ; jump not equal to


mov dx , offset output
mov ah, 9 
int 21h 


mov ax, 4c00h
int 21h

main endp
end main    

已修复!在这里(有很多评论):

.model small

.stack 100h

.data

input  db 30 dup ('$')
n      dw ?
count  dw ?
output db 30 dup ('$')
msj    db 13,10,'The number is = $'

.code
main proc


;INITIALIZE DATA SEGMENT.
    mov ax , @data
    mov ds , ax              


;CAPTURE NUMBER CHAR BY CHAR. NOTICE CHR(13) WILL BE
;STORED IN STRING AND COUNTED.
    mov bx , offset input
    mov count , 0
l1:
    mov ah , 1    
    int 21h               ;CAPTURE ONE CHAR FROM KEYBOARD.
    mov [bx] , al         ;STORE CHAR IN STRING.
    inc bx 
    inc count
    cmp al , 13
    jne l1                ;IF CHAR IS NOT "ENTER", REPEAT.           

    dec count             ;NECESSARY BECAUSE CHR(13) WAS COUNTED.


;CONVERT STRING TO NUMBER. 
    mov bx , offset input ;BX POINTS TO THE FIRST CHAR.
    add bx,  count        ;NOW BX POINTS ONE CHAR AFTER THE LAST ONE.
    mov bp, 0             ;BP WILL BE THE NUMBER CONVERTED FROM STRING.
    mov cx, 0             ;PROCESS STARTS WITH 10^0.
l2:      
;GET CURRENT POWER OF 10.
    cmp cx, 0
    je  first_time        ;FIRST TIME IS 1, BECAUSE 10^0 = 1.
    mov ax, 10
    mul cx                ;CX * 10. NEXT TIME=100, NEXT TIME=1000...
    mov cx, ax            ;CX == 10^CX.
    jmp l22               ;SKIP THE "FIRST TIME" BLOCK.
first_time:    
    mov cx, 1             ;FIRST TIME 10^0 = 1.
l22:    
;CONVERT CURRENT CHAR TO NUMBER.   
    dec bx                ;BX POINTS TO CURRENT CHAR.
    mov al , [bx]         ;AL = CURRENT CHAR.
    sub al , 48           ;CONVERT CHAR TO NUMBER.
;MULTIPLY CURRENT NUMBER BY CURRENT POWER OF 10.
    mov ah, 0             ;CLEAR AH TO USE AX.
    mul cx                ;AX * CX = DX:AX. LET'S IGNORE DX.
    add bp , ax           ;STORE RESULT IN BP.    
;CHECK IF THERE ARE MORE CHARS.    
    dec count
    cmp count , 0
    jne l2


;EXTRACT DIGITS FROM NUMBER ONE BY ONE BY DIVIDING THEM BY 10 AND
;STORING THE REMAINDERS INTO STACK. WE USE THE STACK BECAUSE THE
;STACK STORES DATA IN REVERSE ORDER.
    mov count, 0
    mov ax, bp            ;AX = NUMBER TO PROCESS.
l3:
    mov dx, 0             ;CLEAR DX. NECESSARY FOR DIV.
    mov cx, 10            ;WILL DIVIDE BY 10.    
    div cx                ;DX:AX / 10. RESULT: AX=QUOTIENT, DX=REMAINDER.    
    add dl,48             ;CONVERT DIGIT TO CHAR.
    push dx               ;STORE DIGIT IN STACK.
    inc count
    cmp ax, 0             
    jne l3                ;IF QUOTIENT != 0, REPEAT.


;EXTRACT CHARS FROM STACK IN REVERSE ORDER TO BUILD THE NUMBER IN STRING.
    mov bx, offset output
l4:
    pop dx                ;RETRIEVE ONE CHAR.
    mov [bx], dl          ;STORE CHAR IN STRING.
    inc bx                ;POSITION FOR NEXT CHAR.
    dec count
    jnz l4                ;IF ( COUNT != 0 ) REPEAT.    


;DISPLAY MESSAGE.
    mov dx , offset msj
    mov ah, 9 
    int 21h 


;DISPLAY NUMBER CONVERTED TO STRING.
    mov dx , offset output
    mov ah, 9 
    int 21h 


;WAIT FOR A KEY.
  mov  ah,7
  int  21h


;FINISH PROGRAM.    
    mov ax, 4c00h
    int 21h

main endp
end main