samg
samg

Reputation: 1

copy certain characters from one string to another in assembly

I am trying to loop through a string1 and copy the alpha characters into a string3. The code I have so far is below, probably not the best but it works to a point and puts the letter into the first point of string3. However, the next time around it just overwrites that character until I am left with an empty string at the end. Does anyone know how to fix it so that it will save the element of string1 into the next free space in string3? Many thanks this is driving me crazy!!!

MOV DX,OFFSET STRING1+2 ; dx = address of string1
mov bx, dx       ; bx = dx = address of string1
mov ch, 0      ; set cx to string size for loop 
mov cl, [bx+1] 
; print prompt
call PTHIS       
db  13, 10, "removed non-alphanumeric characters1: ",0

remove_char:
; check if it's not a lower case letter:
cmp byte ptr [bx], 'a'
jg ok3          ; if char is 'a' or above, goto 'ok3'
cmp byte ptr [bx], 'z'
jl ok3                  ; if char is below 'z', goto 'ok3'
cmp [bx],'$'
je '$', next
; ... goto 'ok3' anyway... ("fall through")
inc bx ; next char.

ok3:
push [bx]
lea dx, string3+2 
pop [bx]
mov bh, [bx]
mov string3+2, bh
inc bx ; next char.
loop remove_char

Upvotes: -1

Views: 2143

Answers (1)

Van Uitkon
Van Uitkon

Reputation: 356

After mov cl, [bx+1] check, whether cx is zero. If cx is zero, you have to break the loop at this point, because otherwise, the loop would be repeted 65536 times. Insert this code in the line after mov cl, [bx+1]:

cmp cx, 0
je exit

and insert a label exit (or whatever you want to insert) at the end. This makes sure, that the code is not executed if cx is zero.

Upvotes: 1

Related Questions