Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Need help.. ASAP.. (tasm)

leiyahleiyah Member Posts: 9
i am a beginner of assembly language programming particularly on that with TASM.. We are asked to code a program wherein the user will asked to input a character or a word and then it will print the number of characters inputted by the user.. counting number of characters includes the space(s). here's my code. Kindly help me with this matter.. i really dont know how to fix it..

.MODEL SMALL
.STACK 100h
.DATA

CharPrompt DB 'Enter a word or character: $'
DisplayNumChar DB 13,10,'Number of character is.',13,10, '$'


.CODE
start:
mov ax,@data
mov ds,ax
mov dx,OFFSET CharPrompt
mov ah,9
int 21h
mov ah,10
int 21h


mov ax,@data
mov ds,ax
mov dx,OFFSET DisplayNumChar
mov ah,9
int 21h





mov ah,4ch
mov al,0
int 21h

END start


Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [color=Blue]Check this link for buffered input (INT 21h AH=0Ah) - you are missing the input buffer declaration and use:[/color]

    [link=http://www.ctyme.com/intr/rb-2563.htm]http://www.ctyme.com/intr/rb-2563.htm[/link]

    : [code][size=3]
    : .MODEL SMALL
    : .STACK 100h
    : .DATA
    :
    : [color=Red]MaxChars DB 10
    : NumChars DB 0
    : InputBuffer DB 'xxxx xxxx ' ; Room for 9 chars + CR
    : TextNumChars DB 'x',13,10,'$'[/color]
    :
    : CharPrompt DB 'Enter a word or character: $'
    : DisplayNumChar DB 13,10,'Number of character is.',13,10,
    : '$'
    :
    :
    : .CODE
    : start:
    : mov ax,@data
    : mov ds,ax
    : mov dx,OFFSET CharPrompt
    : mov ah,9
    : int 21h

    : [color=Red]mov dx,OFFSET MaxChars[/color]
    : mov ah,10
    : int 21h
    :
    :
    : [color=Red]; mov ax,@data[/color]
    : [color=Red]; mov ds,ax[/color]
    :[color=Red]; These ^^^ lines needed only once at the start of the code[/color]
    : mov dx,OFFSET DisplayNumChar
    : mov ah,9
    : int 21h
    :
    : [color=Red]; At this point the memory buffer 'InputBuffer' will contain
    : ; the entered characters, terminated with CR symbol (code 13).
    : ; You need to load the address of that buffer into an address
    : ; register (can be BX, SI, DI or BP if I remember correctly)
    : ; and loop through all characters counting them and end the loop
    : ; when the next symbol is 13. Then convert the counter value
    : ; into text (this code will count only up to 9 characters) and
    : ; dump it to console.
    :
    : mov si,OFFSET InputBuffer
    : xor cx,cx ; CX=0 - counter of chars
    : NextChar:
    : lodsb ; Load char into AL and move SI forward by 1 byte
    : cmp al,13 ; Check if the end of buffer reached
    : je Convert ; End of buffer, jump to 'Convert'
    :
    : inc cx ; Count symbols
    : jmp NextChar ; Go back to load next symbol
    :
    : Convert:
    : mov TextNumChars,cl
    : mov dx,OFFSET TextNumChars
    : mov ah,9
    : int 21h[/color]
    :
    : mov ah,4ch
    : mov al,0
    : int 21h
    :
    : END start
    : [/size][/code]
    : [color=Blue]If you want to count more than 9 characters - you need a bigger buffer and more complicated conversion function.[/color]
    :
  • leiyahleiyah Member Posts: 9
    : [color=Blue]Check this link for buffered input (INT 21h AH=0Ah) -
    : you are missing the input buffer declaration and use:[/color]
    :
    : [link=http://www.ctyme.com/intr/rb-2563.htm]http://www.ctyme.com/intr
    : /rb-2563.htm[/link]
    :
    : : [code]: [size=3]
    : : .MODEL SMALL
    : : .STACK 100h
    : : .DATA
    : :
    : : [color=Red]MaxChars DB 10
    : : NumChars DB 0
    : : InputBuffer DB 'xxxx xxxx ' ; Room for 9 chars + CR
    : : TextNumChars DB 'x',13,10,'$'[/color]
    : :
    : : CharPrompt DB 'Enter a word or character: $'
    : : DisplayNumChar DB 13,10,'Number of character is.',13,10,
    : : '$'
    : :
    : :
    : : .CODE
    : : start:
    : : mov ax,@data
    : : mov ds,ax
    : : mov dx,OFFSET CharPrompt
    : : mov ah,9
    : : int 21h
    :
    : : [color=Red]mov dx,OFFSET MaxChars[/color]
    : : mov ah,10
    : : int 21h
    : :
    : :
    : : [color=Red]; mov ax,@data[/color]
    : : [color=Red]; mov ds,ax[/color]
    : :[color=Red]; These ^^^ lines needed only once at the start of the code[/color]
    : : mov dx,OFFSET DisplayNumChar
    : : mov ah,9
    : : int 21h
    : :
    : : [color=Red]; At this point the memory buffer 'InputBuffer' will contain
    : : ; the entered characters, terminated with CR symbol (code 13).
    : : ; You need to load the address of that buffer into an address
    : : ; register (can be BX, SI, DI or BP if I remember correctly)
    : : ; and loop through all characters counting them and end the loop
    : : ; when the next symbol is 13. Then convert the counter value
    : : ; into text (this code will count only up to 9 characters) and
    : : ; dump it to console.
    : :
    : : mov si,OFFSET InputBuffer
    : : xor cx,cx ; CX=0 - counter of chars
    : : NextChar:
    : : lodsb ; Load char into AL and move SI forward by 1 byte
    : : cmp al,13 ; Check if the end of buffer reached
    : : je Convert ; End of buffer, jump to 'Convert'
    : :
    : : inc cx ; Count symbols
    : : jmp NextChar ; Go back to load next symbol
    : :
    : : Convert:
    : : mov TextNumChars,cl
    : : mov dx,OFFSET TextNumChars
    : : mov ah,9
    : : int 21h[/color]
    : :
    : : mov ah,4ch
    : : mov al,0
    : : int 21h
    : :
    : : END start
    : : [/size][/code]:
    : : [color=Blue]If you want to count more than 9 characters - you need a bigger buffer and more complicated conversion function.[/color]
    : :
    :


    i tried to run your code, but it does not return the appropriate output.. i tried to enter my name but then the output is an ascii character.. how's that?

    im really running out of time.. we haven't given enough time to do it.. please help me.. thanks a lot.. you're such a big help..
  • anthrax11anthrax11 Member Posts: 511
    [color=Green]You forgot to convert to ASCII:[/color]
    [code][color=Red]
    Convert:
    [color=Green] add cl, "0"[/color]
    mov TextNumChars,cl
    mov dx,OFFSET TextNumChars
    mov ah,9
    int 21h
    [/color][/code]
  • leiyahleiyah Member Posts: 9
    : [color=Green]You forgot to convert to ASCII:[/color]
    : [code]: [color=Red]
    : Convert:
    : [color=Green] add cl, "0"[/color]
    : mov TextNumChars,cl
    : mov dx,OFFSET TextNumChars
    : mov ah,9
    : int 21h
    : [/color][/code]:
    :

    thank you so much..

    anyway..

    how about if i want the characters to be more than 9..

    what will i do?


  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [color=Blue]This function will convert the value in AX to the buffer:[/color][size=4]
    [code]
    ;
    ; In data section you need this
    ;

    buffer db 'xxxxxx'
    buf_end db '$'

    ;
    ; In code section you need this
    ;

    I2A proc
    mov cx, 10
    mov di, OFFSET buf_end

    Div_by_10:
    xor dx, dx
    div cx

    add dl, '0'
    dec di
    mov [di], dl

    test ax, ax
    jnz Div_by_10

    mov dx, di
    mov ah, 9
    int 21h

    ret
    I2A endp

    ;
    ; To call this function simply pass the value in AX
    ;

    mov ax, 5237
    call I2A
    [/code][/size]
Sign In or Register to comment.