Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

WORKING WITH CHARACTERS, STRINGS AND LISTS TYPE OF DATA

Before starting work with this I would like to mention that you should first
learn about ASCII Encoding.
In ASCII Encoding every character has it corresponding Numeric value. Computer
processes character but not as chracters instead as a Numeric values like UpperCase
'A' = 65. Similarly you can see some of the ASCII Codes below:

A = 65
B = 66
C = 67
D = 68
E = 69
F = 70
G = 71
H = 72
I = 73
J = 74
K = 75
L = 76
M = 77
N = 78
O = 79
P = 80
Q = 81
R = 82
S = 83
T = 84
U = 85
V = 86
W = 87
X = 88
Y = 89
Z = 90

Similarly, for LowerCase Letters:

a = 97
...
z = 122

Now lets move towards our topic.


Well, dealing with Character is quite easy. You should pass in variable as:
character DB 'A'
Always remember that Character will be Always in single quotes. Rest of the
thing will be same as we
used to deal with Numeric data. Now what is list? Well, in previous topic we saw
that we were passing value to a variable as:
num DB 1
Now what can we do in lists is that we can write as:
num DB 1, 2, 4
Here each one data will be of 1 byte. Now a problem here is that how will the
assembler know that 4 is
the ending point of this list. Well, for that we use 0 at the end, so for writing
list we will need to write it as:
num DB 1, 2, 4, 0
Now here 0 specifies that it is the end of the list. It is quite useful in
dealing with Strings as
Strings are also a list of characters. So we will write the String as:
String DB "ABC", 0
Now here is how we write our String Data. Remember that String will be always
in double quotes. Now
lets write the code for this:

section .data

String DB "ABC",0
String2 DB "XYZ",0

section .text
global _start

_start:
MOV bl, [String]
MOV cl, [String2]
MOV eax, 1
INT 80h

Now you understand why I have pushed the Strings inside the bl and cl registers
as I explained from the
last topic. Now if we debug our code and type the command in debugger that is x/x
address[0x0..]
as 0x804a000 in my case for the first one string and if I press enter it will give
me output something like this 0x00434241 in which 43 is A in Hexa and 42 is B and
41 is C Similarly if I type command as:
x/2x address[0x804a000](in my case).
It means that show next block as well, so it will give me the output as:

0x804a000: 0x00434241 0x005a5958


Now here in 0x005a5958 5a is X, 59 is Y and 58 is Z in Hexadecimal System. Now
if I write command as:
x/3x address[0x804a000](in my case)
The output will be as:

0x804a000: 0x00434241 0x005a5958 0x00000000

Now here these zeros are the last zero that I used to specify the end of the list.
Now let me write the code for a character type as well.

section .data

Character DB 'A'

section .text
global _start

_start:
MOV bl, [Character]
MOV eax, 1
INT 80h

Now here is the code for character type of data and the output of this program
will be 65 since 65 is
the ASCII Code for A. Now next code for list.

section .data

List DB 1,2,3,4,0
section .text
global _start

_start:
MOV bl, [List]
MOV eax, 1
INT 80h
Now if I run this code indeed the output will be as 0x04030201. And if I write
the command x/2x indeed
the output will be as:
0x804a000: 0x04030201 0x00000000
And the zeros are ending zero. This is how we get to know that this is the end
of the list or string
etc in memory. Now since we are using debugger but you may find it a bit complex to
deal with. Now to make its interface a bit simpler type a command in terminal that
is follows:

echo "set disassembly-flavor intel" > ~/.gdbinit


After executing this command, now if you start you gdb it will have a bit
simpler and easy to
understand interface.

You might also like