You are on page 1of 15

Dealing with strings as

arrays
By
Prof. Ahmed Fahmy
Letter ASCII Code (Hex.)

0 30

1 31

2 32

3 33

9 39
Letter ASCII Code (Hex.) Letter ASCII Code (Hex.)
a 61 A 41
b 62 B 42
c 63 C 43
. . . .
. . . .
. . . .
z 7A Z 5A
Spacebar 20
Signed Numbers and Unsigned Numbers
• (3)10 = (00000011)2
• (-3)10 = (11111101)2

• Looking to (11111101)2 as a signed number, it is


equivalent to (-3)10
, but looking to it as unsigned number, it is equivalent
to (253)10
Some Selected Jumps
Symbol Unsigned Meaning Signed Meaning
== JE or JZ Jump equal JE or JZ Jump equal

> JA Jump Above JG Jump Greater


≥ JAE Jump Above or equal JGE Jump Greater or Equal

< JB Jump Below JL Jump Less


≤ JBE Jump Below or Equal JLE Jump Less or Equal

!= JNE or JNZ Jump not Equal JNE or JNZ Jump not Equal
String as an array
The following string:
x db “Corona is a very bad virus”
Is equivalent to the following array:
x db ‘C’, ‘o’, ‘r’, ’o’, ‘n’, ‘a’, ‘i’, ‘s’, ‘a’, ‘v’, ‘e’, ‘r’, ‘y’, ‘b’, ‘a’, ‘d’, ‘v’, ‘I’, ‘r’, ‘u’, ‘s’
; upper5.asm
; Change the lower case characters in a string to upper case.
; The string is terminated by dollar sign.
; The first byte in the string is called 'buffer’,
; then print the string after the change
.model small
.stack 32
.data
buffer db "Corona is a very bad virus. Stay at home 24/7", 0ah, 0dh, "$"
message1 db "The string before conversion:", 0ah, 0dh, "$"
message2 db "The string after conversion:", 0ah, 0dh, "$"
Data segment before execution
Data segment after execution
.code
MAIN PROC FAR
PUSH DS
MOV AX, 0
PUSH AX
MOV AX, @DATA
MOV DS, AX

MOV DX, OFFSET message1


CALL print_string

MOV DX, OFFSET buffer


CALL print_string
CALL to_upper

MOV DX, OFFSET message2


CALL print_string

MOV DX, OFFSET buffer


CALL print_string

MOV AH,4CH
INT 21H ;go back to DOS
MAIN ENDP
to_upper proc
push ax
push bx
push cx
MOV BX, offset buffer

yy: MOV AL, [BX]


CMP AL, '$'
JE zz
CMP AL, 'a'
JB xx
CMP AL, 'z'
JA xx
SUB al, 20H
MOV [BX], AL
xx: INC BX
JMP yy
zz: pop cx
pop bx
pop ax
ret
to_upper endp
print_string proc
push ax
push dx

MOV AH, 9
INT 21H ;write the string
pop dx
pop ax
ret
print_string endp

END MAIN

You might also like