Real Newbie Qu: Displaying Text and Numbers?
Author Message
Real Newbie Qu: Displaying Text and Numbers?

Hi

I'd like to display to screen a string and calculated number and then
another string.
I'm using code:

mov bx, 0001h
lea dx, MyString                If I replace MyString with a
mov cx, L_MyString      numeric value and use BYTE as its'
mov ah, 40h             size I get compile errors.
int 21h                 Unfortunately I can't find and example

imple action in the books from
Microsoft. (I'm using MASM 6.1)

Also,  is there a way of  making this a procedure that can be used for
both strings and numeric values?

Thu, 27 Sep 2001 03:00:00 GMT
Real Newbie Qu: Displaying Text and Numbers?

Quote:
> Hi

> I'd like to display to screen a string and calculated number and then
> another string.

To display a number, you convert it to a string, then display the
string.

The simple way of converting a number to a string involves repeated
division by the base in which you're going to display the number
(usually 10).  The remainder is the least significant digit.  You
repeat the operation with the quotient until you get a quotient of 0,
which indicates that you've converted all the digits.

E.g. if you were going to display, 127 in base 10, you'd divide by 10,
getting a remainder of 7 and a quotient of 12.  You'd add '0' to the
0, to get a '7', and make that the last character in your string.
You'd then repeat the same thing -- 12/10 gives a remainder of 1 and a
quotient of 1.  You'd add '0' to 1, and put it in the next-to-last
spot in the string.  You'd repeat the same again: 1/10 gives a
remainder of 1 and a quotient of 0.  You'd add '0' to the 1, and put
it in the third-to-last spot in the string.  Since the quotient is now
zero, you'd done.

Thu, 27 Sep 2001 03:00:00 GMT
Real Newbie Qu: Displaying Text and Numbers?

Quote:

> Hi

> I'd like to display to screen a string and calculated number and then
> another string.
> I'm using code:

>         mov bx, 0001h
>         lea dx, MyString                If I replace MyString with a
>         mov cx, L_MyString      numeric value and use BYTE as its'
>         mov ah, 40h             size I get compile errors.
>         int 21h                 Unfortunately I can't find and example

>                                 imple action in the books from
>                                 Microsoft. (I'm using MASM 6.1)

> Also,  is there a way of  making this a procedure that can be used for
> both strings and numeric values?

The obvious response is to suggest you get hold of a book
that _does_ give useful coding examples?

I used 'Assembly Language for the PC' by Socha and Norton
as an introduction to the masm syntax.

Your code was too incomplete to give a direct response
however the example below might help. It prompts for
the input of two numbers (0 to 65535) and prints the
result. If you want larger numbers or signed numbers
you will need different routines. There is no error
checking included.

.MODEL small
.STACK 256

.DATA

v1      dw      0
v2      dw      0
prompt1 db      '  Enter number: ',0
prompt2 db      'Another number: ',0
prompt3 db      '  add numbers = ',0

.DATA?
Instring        db      256 DUP(?)

CODESEG

main    proc
mov     ax,DGROUP       ;put data segment into ax
mov     ds,ax           ;ds -> data segment
mov     es,ax           ;es -> data segment
;--------------------------------------------------
mov     si,offset prompt1
call    PrintString

mov     di,offset Instring
call    CRLF                ;INPUT Instring\$
mov     si,offset Instring
call    StrToNum            ;ax = VAL(InString\$)
mov     [v1],ax             ;v1 = ax

mov     si,offset prompt2
call    PrintString

mov     di,offset Instring
call    CRLF
mov     si,offset Instring
call    StrToNum
mov     [v2],ax             ;v2 = ax

add     ax,[v1]                ;ax = ax + v1

mov     si,offset prompt3
call    PrintString

mov     si,offset Instring
call    NumToStr            ;InString\$ = STRING\$(ax)
mov     si,offset Instring
call    PrintString
call    CRLF                ;PRINT InString\$

;--------------------------------------------------
mov     ax,4C00h
int     21h
main    endp

;** READ KEYBOARD TO STRING POINTED TO BY DI **

push    ax
push    di
push    bx
mov     bx,di
NextChar:
sub     ah,ah
int     16h     ;wait for key function
cmp     al,13   ;[ENTER] key?
jz      Done
cmp     al,8    ;[<-] key?
jz      BackSpace
mov     [di],al ;save character
inc     di
mov     dl,al   ;
mov     ah,2
int     21h     ;echo character
jmp     NextChar

BackSpace:
cmp     di,bx   ;at beginning?
jz      NextChar        ;yep no back space
dec     di
mov     ah,2
mov     dl,8
int     21h     ;backspace
mov     ah,2
mov     dl,' ';
int     21h     ;PRINT " ";
mov     ah,2
mov     dl,8
int     21h     ;backspace
jmp     NextChar

Done:
sub     al,al   ;clear al
mov     [di],al ;null string
pop     bx
pop     di
pop     ax
ret

;** PRINT STRING POINTED TO BY SI **
PrintString     proc
push    ax
push    si
PrintLoop:
mov     al,[si]
inc     si
or      al,al   ;test for al=0
jz      Finish
mov     dl,al
mov     ah,2
int     21h     ;print character
jmp     PrintLoop
Finish:
pop     si
pop     ax
ret
PrintString     endp

CRLF    proc
push    ax
push    dx
mov     ah,2
mov     dl,13
int     21h
mov     dl,10   ;lf
int     21h
pop     dx
pop     ax
ret
CRLF    endp

;** Convert string pointed to by SI to number in ax **
StrToNum        proc
push    bx
push    cx
push    dx
push    si
sub     cx,cx   ;cx = 0
mov     bx,10   ;base 10
NextDigit:
mov     al,[si] ;get digit
inc     si
sub     ah,ah   ;ah = 0
sub     al,'0'  ;convert to 0-9 value
jc      NotDecDigit
cmp     al,10   ;
ja      NotDecDigit
xchg    ax,cx   ;ax = old value cx = new value
mul     bx      ;ax = ax * 10
jmp     NextDigit
NotDecDigit:
mov     ax,cx   ;ax = result
pop     si
pop     dx
pop     cx
pop     bx
ret
StrToNum        endp

;** Convert number in ax to string pointed to by DI **
NumToStr        proc
push    ax
push    dx
push    cx
push    bx
push    di
mov     bx,10
xor     cx,cx
NotZero:
xor     dx,dx   ;set upper word of N to 0
div     bx      ;N/10 and n mod 10
add     dl,'0'  ;remainder in dl convert to asc
push    dx      ;push one digit onto stack
inc     cx      ;count digits
or      ax,ax   ;n = 0?
jne     NotZero
PopDigit:
pop     ax      ;last digit first
mov     [di],al ;
inc     di
loop    PopDigit
sub     al,al   ;al = 0
mov     [di],al ;null string
pop     di
pop     bx
pop     cx
pop     dx
pop     ax
ret
NumToStr        endp

END     main

Thu, 27 Sep 2001 03:00:00 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages