2

I am trying to convert string into integer and again back to string. but it is giving some weird value, I have update an try something else please check the code again. why the output is coming not right ?

Little more update but still not right input

.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 ax , 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 ; decrement 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    
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Muzammil Bilwani
  • 146
  • 4
  • 14
  • Your code needs to be cleaned up as well so that it formats it correctly. Normally I'd just do it for you but I seem to have lost my edit post right somehow. – David Hoelzer Apr 30 '15 at 21:20
  • 'l2 : mov dx , [bx] add dx , 48 mov cx , 10 mul cx add ax , [bx] inc bx dec count cmp count , 0 jne l2 ' – Muzammil Bilwani May 04 '15 at 19:21

1 Answers1

1

Fixed! Here it is (with lots of comments):

.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    
  • As shown in the 16-bit asm loop in [connecting integer multiplication in assembly x86](https://stackoverflow.com/a/74709668), `total = total*10 + digit` only needs one multiply in the loop, and is simpler to code, no need to branch on the first iteration being special. – Peter Cordes Dec 06 '22 at 22:48
  • @PeterCordes, originality is important, I dont want to be called imitator. – Jose Manuel Abarca Rodríguez Dec 07 '22 at 16:27
  • `total += digit * power; power *= base;` is also a known algorithm, it's just worse (for efficiency and code size), which is why it's rarely used. Also it has to read the number backwards, starting with the least-significant digit, which is extra inconvenient when reading input 1 char at a time. – Peter Cordes Dec 07 '22 at 16:43
  • 1
    IMO, creativity is good when it leads to finding *more* efficient ways to do things, like combining two standard things (reading input and `atoi`) into one efficient loop. Especially for a Stack Overflow answer for beginners to learn from, coming up with a more complex and confusing way isn't a good example. (Unless you want to write an answer that compares it against a standard way to discuss the differences, to help beginners see there are multiple ways to do things, and what makes one way better.) Again, that's my opinion. – Peter Cordes Dec 07 '22 at 16:44
  • @PeterCordes, you are affecting my selfsteem. – Jose Manuel Abarca Rodríguez Dec 07 '22 at 16:49
  • 1
    Sorry, about that; I should have phrased that more constructively. Originality is great when you're learning things yourself; coming up with your own way to do something forces you to really think about the problem, and is good for building skills to solve other problems that aren't standard/well-known. Even if you don't come up with the most efficient solution. But unfortunately the results of that that don't always make the best example for others to learn from, since it can involve more steps for a beginner to try to follow. – Peter Cordes Dec 07 '22 at 17:03
  • @PeterCordes, I feel less worse now, thanks. – Jose Manuel Abarca Rodríguez Dec 07 '22 at 17:59