ASM - print a string ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ Well, here's the classic example for the first program in just about every language. It prints a message to the screen by using a DOS function. More specifically, it uses function 9 of interrupt 21h. Here's the mock specification for the function: þ|IN: | | | |OUT: þah = 9 ;ah tells INT 21h which function you want DS:DX = FAR pointer to the string to be printed. ;the string must terminate with a dollar sign ($) Prints the string to the screen

Other than that function, there's nothing new that can't easily be figured out. The directive SEG, as you might have guessed, returns the segment that the specified label is in. OFFSET returns the offset from the begining of the segment to the specified label. Together, you can form a FAR pointer to a specified label. Another thing you might wonder about is why I put the SEG Message into AX and THEN Put it in DS. The answer is: You have to. An immediate value cannot be put into a segment register, but a register or an indexed value can. For instance: These are legal: : : mov mov DS,AX DS,[TheSegment]

But these are not: : : mov mov DS,50 DS,0a000h

One last piece of info: in the lines: :Message : db db "This was printed using function 9 " "of the DOS interrupt 21h.$" Its the same as a CHAR in C, which is 1 byte

The DB is just a data type. in length. Other common data types are: DW DD

same as an INT in C - 2 bytes same as a double int or long int or a FAR pointer - 4 bytes

Well, that's pretty much it for this short section... Try playing around with the 'print' function... Ya learn best by playing with it. One last side note: I COULD have put the message in the CODE segment instead, by doing this: ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ DOSSEG Page 1

DATA Message .CODE Message START: mov mov mov mov int mov int END START ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ The advantage to having all your data in the CODE segment is that DS and ES can be pointing anywhere and you can still access your data via a segment override! Example: say I'm in the middle of copying one section of the screen memory to another and I need to access the variable "NumLines" I'd do it like this: ÄÄÄÄÄÄÄÄ mov ax.moves ax into ds (ds=ax) .[CS:NumLines] in.CODE START: mov mov mov mov int ax. ax. It requires a FAR pointer to .offset Message .MODEL SMALL .MUST be here! Program will crash without it! dx.4c00h 21h ..offset Message ah.$" db "Hey look! I'm in the code segment!$" .9 21h ax.STACK 200h . eh? ÚÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ ³ .seg Message ds.terminates with a "$".MODEL SMALL .cs ds.seg Message db db "This was printed using function 9 " "of the DOS interrupt 21h.9 .Returns control to DOS .ASM ³ ÀÄÄÄÄÄÄÄÄÄÄÙ DOSSEG .since CS already points to the same segment as cannot do this -> mov ds.STACK 200h .this is in IDEAL mode ^^^ ÄÄÄÄÄÄÄÄ Code Segment override Pretty flexable.what is to be printed in DS:DX Page 2 . .ax dx.I don't have to explicitly load the segment that message .ASM print a string .Function 9 of DOS interupt 21h prints a string that 21h .move the OFFSET of `Message' into DX ah.moves the SEGMENT that `Message' is in into AX .

ASM print a string .MUST be here! Program will crash without it! Page 3 .mov int END START ax.Returns control to DOS .4c00h 21h ASM1.

Sign up to vote on this title
UsefulNot useful