Assembly Language 3

6
Course Name : Computer Organisation Course Code : TDB1123/TCB1043 Date Assigned : Week 11 ________________________________________________________________________ 1.0 Displaying a String In our first program, we used INT 21h, functions 1 and 2, to read and display a single character. Here is another INT 21h function that can be used to display a character string: INT 21h, Function 9: Display a String Input : DX = offset address of string. The string must end with a ‘$’ character. The ‘$’ marks the end of the string and is not displayed. If the string contains the ASCII code of a control character, the control function is performed. To demonstrate this function, we will write a program that prints “HELLO!” on the screen. This message is defined in the data segment as MSG DB ‘HELLO!$’ 1.1 The LEA Instruction INT 21h, function 9, expects the offset address of the character string to be in DX. To get there, we use a new instruction: LEA destination, source Where destination is a general register and source is a memory location. LEA stands for “Load Effective Address”. It puts a copy of the source- offset address into the destination. For example, LEA DX, MSG Puts the offset address of the variable MSG into DX. Because our second program contains a data segment, it will begin with instructions that initialize DS. The following paragraph explains why these instructions are needed. 1.2 Program Segment Prefix __________________________________________________________________________________________ __________________ Saipunidzam Mahamad @ Computer Organisation ©

description

Computer is very interesting when you know about assembly language..

Transcript of Assembly Language 3

Course Name : Computer OrganisationCourse Code : TDB1123/TCB1043Date Assigned : Week 11________________________________________________________________________1.0 Displaying a StringIn our first program, we used INT 21h, functions 1 and 2, to read and display a single character. Here isanother INT 21h function that can be used to display a character string:INT 21h, Function 9:Display a StringInput : D ! offset address of string. The string must end with a "#$ character.The "#$ mar%s the end of the string and is not displayed. If the string contains the &'(II code of a controlcharacter, the control function is performed.To demonstrate this function, we will write a program that prints )H*++,-. on the screen. This message isdefined in the data segment as /'0 D1 "H*++,-#$1.1 The LE InstructionINT 21h, function 2, e3pects the offset address of the character string to be in D. To get there, we use anew instruction:+*& destination, source4here destination is a general register and source is a memory location. LE stands for )+oad *ffecti5e&ddress.. It puts a copy of the source6offset address into the destination. 7or e3ample,+*&D, /'08uts the offset address of the 5ariable /'0 into D.1ecause our second program contains a data segment, it will begin with instructions that initiali9e D'. Thefollowing paragraph e3plains why these instructions are needed.1.2 !rogra" Seg"ent !re#i$4hen a program is loaded in memory, D,' prefaces it with a 2:;6byte progra" seg"ent pre#i$ %!S!&.The8'8containsinformationabout theprogram. Theresult isthat D'doesnot containthesegmentnumber of the data segment. To correct this, a program containing a data segment begins with these twoinstructions:/,< &, =D&T&/,< D', &=D&T& is the name of the data segment defined by D&T&. The assembler translates the name =D&T&into a segment number. Two instructions are needed because a number >the data segment number? may notbe mo5ed directly into a segment register.4ith D' initiali9ed, we may print the )H*++,-. message by placing its address in D and e3ecuting INT21h:+*& D, /'0 @ get message/,< &H, 2 @ display string function____________________________________________________________________________________________________________Saipunidzam Mahamad @ Computer Organisation INT 21h @ display string1.'(ase (on)ersion !rogra"4e will now combine most of the material co5ered in this chapter into a single program. This programbegins by prompting the user to enter a lowercase letter, and on the ne3t line displays another message withthe letter in uppercase. 7or e3ample, *NT*A & +,4*A(&'* +*TT*A: aIN B88*A (&'* IT I': &4e use *CB to define (A and +7 as names for the constants DDH and D&H.(A *CB DDH+7 *CB D&HThe messages and the input character can be stored in the data segment li%e this:/'01 D1 "*NT*A & +,4*A (&'* +*TT*A: #$/'02 D1 DDH, D&H, "IN B88*A (&'* IT I': "(H&A D1 E, "#$In defining /'02 and (H&A, we ha5e used a helpful tric%: because the program is supposed todisplay the second message and the letter >after con5ersion to upper case? on the ne3t line, /'02 startswith the &'(II codes for carriage return and line feed@ when /'02 is displayed with INT 21h, function 2,these control functions are e3ecuted and the output is displayed on the ne3t line. 1ecause /'02 does notend with "#$,INT 21h goes on and displays the character stored in (H&A.,ur program begins by displaying the first message and reading the character:+*& D, /'01 @ get first message/,< &H, 2 @ display string functionINT21H @ display first message/,< &H, 1 @ read character functionINT21H @ read a small letter into &+Ha5ing read a lowercase letter, the program must con5ert it to upper case. In the &'(II character seFuence,the lowercase letters begin at ;1h and the uppercase letters start at G1h, so subtraction of 2Dh from thecontents of &+ does the con5ersion:'B1&+, 2DH @ con5ert it to upper case/,< (H&A, &+ @ and store itNow the program displays the second message and the uppercase letter:+*& D, /'02 @ get second message/,< &H, 2 @ display string functionINT 21H @ display message and upper case letter in front____________________________________________________________________________________________________________Saipunidzam Mahamad @ Computer Organisation 2.0 E$ercises2.1 sse"*ly progra" to print a stringTIT+* 80/H2 : 8AINT 'TAIN0 8A,0A&/./,D*+'/&++.'T&(I1DDH.D&T&/'0D1 "H*++,-#$.(,D*/&IN 8A,(@ initiali9e D'/,< &, =D&T&/,