3

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?

0

2 Answers 2

5

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
Sign up to request clarification or add additional context in comments.

1 Comment

It is more complicated with word-sized elements, because different assemblers use different conventions, see euroassembler.eu/eadoc/#CharacterConstans
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.