1 ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
\r
2 ASM1.ASM - print a string
\r
3 ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
\r
5 Well, here's the classic example for the first program in just about
\r
6 every language. It prints a message to the screen by using a DOS function.
\r
7 More specifically, it uses function 9 of interrupt 21h. Here's the mock
\r
8 specification for the function:
\r
11 |IN: ah = 9 ;ah tells INT 21h which function you want
\r
12 | DS:DX = FAR pointer to the string to be printed.
\r
13 | ;the string must terminate with a dollar sign ($)
\r
15 |OUT: Prints the string to the screen
\r
18 Other than that function, there's nothing new that can't easily be
\r
19 figured out. The directive SEG, as you might have guessed, returns the
\r
20 segment that the specified label is in. OFFSET returns the offset from
\r
21 the begining of the segment to the specified label. Together, you can
\r
22 form a FAR pointer to a specified label.
\r
24 Another thing you might wonder about is why I put the SEG Message into
\r
25 AX and THEN Put it in DS.
\r
27 The answer is: You have to. An immediate value cannot be put into a
\r
28 segment register, but a register or an indexed value can. For instance:
\r
33 : mov DS,[TheSegment]
\r
40 One last piece of info: in the lines:
\r
42 :Message db "This was printed using function 9 "
\r
43 : db "of the DOS interrupt 21h.$"
\r
45 The DB is just a data type. Its the same as a CHAR in C, which is 1 byte
\r
48 Other common data types are:
\r
50 DW same as an INT in C - 2 bytes
\r
51 DD same as a double int or long int or a FAR pointer - 4 bytes
\r
54 Well, that's pretty much it for this short section... Try playing around
\r
55 with the 'print' function... Ya learn best by playing with it.
\r
59 I COULD have put the message in the CODE segment instead, by doing this:
\r
61 ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
\r
68 Message db "Hey look! I'm in the code segment!$"
\r
71 mov ax,cs ;since CS already points to the same segment as Message,
\r
72 mov ds,ax ;I don't have to explicitly load the segment that message
\r
75 mov dx,offset Message
\r
79 mov ax,4c00h ;Returns control to DOS
\r
80 int 21h ;MUST be here! Program will crash without it!
\r
84 ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
\r
86 The advantage to having all your data in the CODE segment is that DS and
\r
87 ES can be pointing anywhere and you can still access your data via a segment
\r
91 say I'm in the middle of copying one section of the screen memory to
\r
92 another and I need to access the variable "NumLines" I'd do it like this:
\r
96 mov ax,[CS:NumLines] ;this is in IDEAL mode
\r
98 ÄÄÄÄÄÄÄÄ Code Segment override
\r
100 Pretty flexable, eh?
\r
103 ÚÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
\r
112 Message db "This was printed using function 9 "
\r
113 db "of the DOS interrupt 21h.$"
\r
118 mov ax,seg Message ;moves the SEGMENT that `Message' is in into AX
\r
119 mov ds,ax ;moves ax into ds (ds=ax)
\r
120 ;you cannot do this -> mov ds,seg Message
\r
122 mov dx,offset Message ;move the OFFSET of `Message' into DX
\r
123 mov ah,9 ;Function 9 of DOS interupt 21h prints a string that
\r
124 int 21h ;terminates with a "$". It requires a FAR pointer to
\r
125 ;what is to be printed in DS:DX
\r
127 mov ax,4c00h ;Returns control to DOS
\r
128 int 21h ;MUST be here! Program will crash without it!
\r