Wednesday, November 5, 2008

String Manipulation

String manipulation in assembly is quite confusing sometimes, when i was making an assembly program for it

this is how you declare a string

string db 12,10 dup 0


In this example a variable string is declared in byte, we want the string to have 10 elements, but in assembly you have to add 2 more elements those we 12 elements in total. The index [0] of the string hold the maximum size of the array which is 10 and index [1] holds that actual size of the string that the user inputed.

in this sample program, we want a user to input a string and displays the string, displays the alphabet characters, numbers, and special characters.


jmp start
string db 100,101 dup 0
mess1 db 10,13," Enter a string: $"
mess2 db 10,13, " Inputted String: $"
alphamess db 10,13, " Alphabet letters: $"
nummess db 10,13, " Numerical Values: $"
charmess db 10,13, " Special characters: $"
contmess db 10,13, " Do you want to continue (y/n): $"
space db " $"
newline db 10,13,"$"
start:
xor ax,ax
xor bx,bx
xor dx,dx
xor cx,cx
xor si,si
xor di,di

mov ax,3
int 10h

call new_Line

lea dx,mess1
mov ah,9
int 21h

lea dx,string
mov ah,10
int 21h
;------------------------displaying string----------------------
call new_Line
lea dx,mess2
mov ah,9
int 21h

lea si,string+1
mov cl,[si]
lea di,string+2
cmp cl,0
je start
display:
mov dl,[di]
mov ah,2
int 21h
inc di
loop display

;------------------------Sorting alpha-----------------------
lea dx,alphamess
mov ah,9
int 21h

lea si,string+1
mov cl,[si]
lea di,string+2

alphasorting:
mov al,[di]
cmp al,65
jl safety

cmp al,122
jg safety
cmp al,91
je safety
cmp al,92
je safety
cmp al,93
je safety
cmp al,95
je safety
cmp al,96
je safety

mov dl,al
mov ah,2
int 21h
call spacer
safety:
inc di
loop alphasorting

;------------------------Sorting Numerical-----------------------
lea dx,nummess
mov ah,9
int 21h

lea si,string+1
mov cl,[si]
lea di,string+2

numsorting:
mov al,[di]

cmp al,48
jl numsafety
cmp al,57
jg numsafety

mov dl,al
mov ah,2
int 21h
call spacer

numsafety:
inc di
loop numsorting

;-----------------------special character sorting-------------------
lea dx,charmess
mov ah,9
int 21h

lea si,string+1
mov cl,[si]
lea di,string+2

charsorting:
mov al,[di]

cmp al,32
je charsafety
cmp al,58
je specialchar
cmp al,59
je specialchar
cmp al,60
je specialchar
cmp al,61
je specialchar
cmp al,62
je specialchar
cmp al,63
je specialchar
cmp al,64
je specialchar
cmp al,91
je specialchar
cmp al,92
je specialchar
cmp al,93
je specialchar
cmp al,94
je specialchar
cmp al,95
je specialchar
cmp al,96
je specialchar
cmp al,58
je specialchar
cmp al,48
jl specialchar
cmp al,122
jle charsafety

specialchar:
mov dl,al
mov ah,2
int 21h
call spacer

charsafety:
inc di
loop charsorting

continue:
call new_Line
xor dx,dx

lea dx,contmess
mov ah,9
int 21h

mov ah,1
int 21h

cmp al,'y'
je start_bridge
cmp al,'n'
je quit

jmp continue
quit:
int 20h
new_Line:
lea dx,newline
mov ah,9
int 21h
spacer:
lea dx,space
mov ah,9
int 21h

ret
start_bridge:
call start

1 comment:

Monsieur RaKah said...

Q: Do that ASM code use DEBUG to be executable? :)

it's nice to know you and i'm glad to have your explanation.