-2

maybe someone can help me. I'm trying to print '' as many as user entered. So I need to convert string to number. I'm trying to do it in loop but have some problems my code is just not working properly it's printing 65535 '' basically it is the maximum. Maybe someone can explain where is my problem?

.MODEL small

.STACK 200h

.DATA

 input DB 4, ? , 4 dup (?)
 Symbol DB '*$'
.CODE

Start: 
  
mov AH,0Ah 
mov ds, ax
mov DX, offset input
int 21h

  mov cl, input[2]
   mov si,2
    mov ax,0

   LoopStart:
   mov bh,10
   mul bh
   mov ah,input[si]
   add ah,al
   sub cl,1
   cmp cl,0
   jne LoopStart

    mov cx,ax
   mov ax, @DATA
   mov ds, ax
    mov dx, offset Symbol
    LoopStart1:
    mov ah, 9 
    int 21h;  
    sub cx,1
    cmp cx,0
    jne LoopStart1



    mov ah, 4Ch
    int 21h

END Start
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
TomLynx23
  • 113
  • 1
  • 3
  • `mul bh` does `AX = AL * BH`. It makes no sense to add the two halves of the multiply result together. For working code, see [NASM Assembly convert input to integer?](https://stackoverflow.com/q/19309749) (should be easy to port to 16-bit) – Peter Cordes Nov 01 '20 at 10:05
  • I got stuck earlier on the input part. I'm pretty sure `mov ah, 0ah; mov ds, ax` is going to set your ds to something weird. Are you actually sure your input is working? Try just printing back the string you got... –  Nov 01 '20 at 10:10
  • @dratenik: or single-step with a debugger so you can examine memory and registers. But good point, `mov ah, 0ah` looks unlikely to be the right segment base; it's not fixed. (AL will have some fixed value on entry to an EXE, IDK what. Unless hyper-optimizing for code-size, not a good idea to rely on it.) – Peter Cordes Nov 01 '20 at 10:11
  • The setup `mov ax, @DATA; mov ds, ax` in the middle is probably the thing you want, move it to the start and then never touch ds again. And if your maximum is 66536, why is the input call only asking for 4 characters? –  Nov 01 '20 at 10:45

1 Answers1

0

So here will be the working solution

 .MODEL small
.STACK 200h
.DATA
Symbol DB '*$'
 input DB 6, ? , 6 dup (?)
.CODE
Start: 

   mov ax, @DATA
   mov ds, ax
    mov AH,0Ah
    mov DX, offset input
    int 21h

    mov AH, 0Fh
    int 10h         
    mov AH,0        
    int 10h         

   mov si,2
   mov ax,0
   mov cl,input[1]

LoopStart:
   mov bh,10
   mul bh

   add al,input[si]
   sub al,48
   add si,1
   sub cl,1
   ; cmp cl,0
   
   jne LoopStart

    
  
   mov cx,ax

   
    mov dx, offset Symbol
    LoopStart1:
    mov ah, 9 
    int 21h;  
    sub cx,1
    cmp cx,0
    jne LoopStart1

    mov ah, 0
    int 16h

    mov ah, 4Ch
    int 21h
 
END Start
TomLynx23
  • 113
  • 1
  • 3
  • This will fail for some large inputs, e.g. ones where `add al,input[si]` wraps without carry propagation into AH. Of course, using an 8-bit `mul` means it only works for at most 4-bit numbers where the leading 3 decimal digits are <= 255. Otherwise doing `AX = AL*10` at the top of each iteration will be throwing away non-zero bits from AH. – Peter Cordes Jan 09 '22 at 21:08
  • [connecting integer multiplication in assembly x86](https://stackoverflow.com/a/74709668) has a loop that uses 16-bit multiply and add. – Peter Cordes Dec 06 '22 at 22:55