Question:
I have assembly code to calcaulate the string length but it work for only 99 I need it to work for above 100 ?
safdar ALI
2008-07-10 02:30:30 UTC
title calculate the length of a string and display ax contents
.model large
.stack 100h

.data
message db "hdfddddddddddfffffffffffffffffffffffffffffd$"

.code
main proc

mov ax, @data
mov ds,ax

mov cx, 0
mov bx, offset message

begin:
inc cx
cmp byte ptr [bx], '$'
je finish
inc bx
jmp begin
finish:

mov ax,cx
mov bl,10
div bl

mov dl,al ; al = result of division
add dl,48

mov ch,ah ; to save the remainder

mov ah,2
int 21h

mov dl,ch
add dl,48
mov ah,2
int 21h

mov ax,4c00h
int 21h

main endp
end main
Three answers:
balk
2008-07-11 08:39:04 UTC
Wait a minute. All you want to do is calculate the length of a '$' terminated string and print out the value? (Most people use a 0 to terminate strings.) Anyway, the problem is that you increment CX at the start of the loop. You should put it after 'JE finish.' Here's another way of writing your program:



.286

.model tiny

.data

message db "asdfasdfasdfasdf",'$'



.code

org 100h

xor ax,ax ;use xor to efficiently zero out registers

mov bx,10

mov dx,offset message

push 0ffffh



even

myloop:

cmp byte ptr ds:[dx],'$'

je finish

inc ax ;increment counter

inc dx ;increment 'message' pointer

jmp myloop



even

finish:

xor dx,dx

div bx

test ax,ax ;does AX=0?

jz divdone

push dx ;push digit onto stack

jmp finish



even

divdone: ;one more push

push dx

mov al,2



even

print:

pop dx

cmp dx,0ffffh

je getout

add dl,48

int 21h

jmp print



even

getout:

mov ax,4c00h

int 21h

end



Use the 'even' directive on address labels and data labels to ensure that they start on even addresses for faster access. You could use one of the string CPU instructions (scasb,cmpsb) with the 'rep' prefix for finding the end of the string with the count in CX, but for small strings, that method is detrimental (speedwise) for 486 and Pentium computers.



Want an 'integer to decimal ASCII' converter procedure?

Here:

even

itoa proc normal nolanguage

;Convert unsigned word to decimal ASCIIZ string in numberbuffer.

;INPUT: AX = unsigned word

;OUTPUT: numberbuffer = ASCIIZ string

; CX = # of digits (not including terminating 0)

mov bx,10

push 0ffffh

mov si,offset numberbuffer

xor cx,cx



even

@@idiv:

xor dx,dx

div bx

test ax,ax

jz @@idivdone

push dx

jmp @@idiv



even

@@idivdone:

push dx



even

@@ipop:

pop dx

cmp dx,0ffffh

je @@ifix

inc cx

add dl,48

mov byte ptr ds:[si],dl

inc si

jmp @@ipop



even

@@ifix:

mov byte ptr ds:[si],0

ret

itoa endp
roderick_young
2008-07-10 09:50:08 UTC
This code will print 03 for length of 3, hope that's ok with you. Otherwise, you need to test the first digit for zero before printing.



What you want to do here is divide by 100 the first time, print the 100's digit, then with the remainder, divide by 10. Or, if you want to print 4 digits, then divide by 1000, print a digit, then 100, print a digit, then 10, print a digit, and print the final remainder.



Note that your program is only good for 16-bit values, so a string longer than 65535 will wrap around with the indexing you're using, and the program will loop forever.



There is also a recursive method of printing a number that uses the stack. It's quite elegant, automatically supresses leading zeroes, and works for as many digits as will fit in an integer size.



Algorithm:



DecPrint(unsigned ax) {

if (ax/10 !=0)

DecPrint(ax / 10);



PrintDigit(ax % 10);

}



; input argument is unsigned in ax



DecPrint:

mov dx,0 ; must do 32-bit divide, or could overflow

mov bx,10

div bx ; ax = quotient, dx = remainder

and ax,ax ; if quotient is 0, don't need to print another level

je finish



push dx ; recursive call to self

call DecPrint

pop dx



finish:

add dl,'0' ; convert to ASCII

mov ah,2 ; DOS char print

int 21h



ret
2008-07-11 08:42:37 UTC
♠ this is very good, but you should exclude last $ from your count;

so move ‘inc cx’ statement after ‘inc bx’;

♣ also see my previous answer!

https://answersrip.com/question/index?qid=20080709230053AA20mCq


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...