Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between String and Array in assembly language

I'm currently doing assembly programming (16-bit) on DOSBox using MASM.

What I know is:

This is how you declare a string:

var db 'abcde'

This is how you declare an array:

var db 'a','b','c'

I don't know for sure if these are correct, and I'm confused between these two, array and string

mov ah,9
int 21h

Does above code show output string and not output array?

like image 280
Hassaan Raza Avatar asked Apr 14 '26 05:04

Hassaan Raza


2 Answers

Every string can be seen as an array of bytes.

aString     db "abcdef", 13, 10, "$"
byteArray   db "a", "b", "c", "d", "e", "f", 13, 10, "$"

You can output both in the same way:

mov     dx, offset aString
mov     ah, 09h    ; DOS.PrintString
int     21h

mov     dx, offset byteArray
mov     ah, 09h    ; DOS.PrintString
int     21h

This works because the elements in an array follow each other close in memory and so there's no real difference in the storage for aString and the storage for byteArray.

What helps to differentiate is that when people talk about an array they are mostly interested in the numerical value that is stored in the array element as opposed to when they talk about a string they don't care about the actual ASCII code for the characters that make up the string.

In aString db "abcdef", 13, 10, "$" we see the characters a, b, ...
In byteArray db "a", "b", "c", "d", "e", "f", 13, 10, "$" we rather see the numbers 97, 98, ... (Normally we would also have written it with numbers to start with!)


But not every array is a string because you can have arrays with word-sized elements, or dword-sized elements.

byteArray  db 1, 2, 3     <== 3 bytes storage
wordArray  dw 1, 2, 3     <== 6 bytes storage
dwordArray dd 1, 2, 3     <== 12 bytes storage
like image 105
Sep Roland Avatar answered Apr 15 '26 19:04

Sep Roland


There is literally no difference; they both assemble the same bytes of data into the output file. (Or they would if you included the 'd' and 'e' in the "array" version.)

I think MASM's SIZEOF operator will include the whole line of declarations either way.

Strings are a special case of arrays, basically just a convenient syntax for giving multiple characters to one db directive.


Note that sometimes the word "string" implies an implicit-length string, with a 0 or '$' byte as the terminator. So you can pass just a pointer to the start, instead of pointer + length for an explicit-length string.

like image 42
Peter Cordes Avatar answered Apr 15 '26 20:04

Peter Cordes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!