150 likes | 231 Views
In Class Program. Write, assemble and test a program: Use the DB directive to define the following list of numbers and name it array: 31h, 32h, 33h, 34h
E N D
In Class Program • Write, assemble and test a program: • Use the DB directive to define the following list of numbers and name it array: • 31h, 32h, 33h, 34h • Write instructions to load each number into DL and display it on the console. (use Int 21h, function 2h to display the byte in DL on the console) • Explain why the output is “1234”
Procedure • Get template • Make necessary changes. • Assemble and Link using make16 • Run and debug using Codeview
Template for Assembler Programs (Spring, 2003) ;*********************************************** ; Program Name * ;*********************************************** title INCLUDE Irvine16.inc ;*********************************************** ; Data Segment * ;*********************************************** .data ; put data here ;*********************************************** ; Code Segment * ;*********************************************** .code main proc mov ax,@data mov ds,ax ;initialize DS register ; put code here mov ah,4Ch ;int 21h function 4c int 21h ;return to DOS main endp end main ;end of program
Defining Data • .data • List byte 31h, 32h, 33h, 34h
Writing to Standard Output • Interrupt 21h (MS-DOS Services) • Function 2 • Write character stored in DL to standard out (Appendix C-2, page 652 of Irvine 4th Edition) • We have to get the data from memory to register DL and then Mov ah,2 ; use the mov instruction to ; store the interrupt 21h ; function number in register AH Int 21h ; use int instruction to process ; interrupt
Moving Data from Memory to DL • MOV destination, source • The source operand is unchanged! • Both operands must be the same size • Both operands cannot be memory operands! • CS and IP cannot be destination operands • An immediate value cannot be moved to a segment register
MOV Instruction • MOV reg, reg • MOV mem, reg • MOV reg, mem • MOV mem, imm • MOV reg, imm • In real address mode • MOV r/m16, sreg • MOV sreg, r/m16
Moving Data from Memory to DL • Mov dl, list ; AL = 31h • Mov dl, list + 1 ; AL = 32h • Mov dl, list + 2 ; AL = 33h • Mov dl, list + 3 ; AL = 34h
Using Direct-Offset Operands • The data in list (31h, 32h, 33h, 34h) can be accessed using the label (list). • Putting brackets around the source operand does not affect the outcome. They are not required by MASM. • List+1 == [List+1] • Use List+2, List+4, etc… for WORD lists • Use List+4, List+8, etc… for DWORD lists
Title Writing_1234_to_Screen (1234_2.ASM) INCLUDE irvine16.inc .data array db 31h,32h,33h,34h ;use db to define each element as 1 byte .code main proc mov ax,@data ;copy the address of the data mov ds,ax ;segment(@data) into the DS register mov ah,2 ;int 21 function 2 displays the character in DL mov dl, array ;copy 1st byte of array to dl int 21h mov dl, array+1 ;copy 2nd byte to dl - uses direct-offset to access list elements int 21h mov dl, array+2 ;copy 3rd byte to dl int 21h mov dl, array+3 ;copy 4th byte to dl int 21h mov ax, 4c00h ;terminate program and return to DOS int 21h main endp end main
Can INC instruction be used to access different elements of the list ? • The INC instruction adds 1 to a single operand • INC reg/mem • So can we use • INC array (example program) • No! The only practical way to handle an array or list is to use a register as a pointer and change the register value to point to different elements of the list.
OFFSET Operator • Returns the offset of a data label. • In protected mode, an offset is always 32-bits long • In real mode, an offset is always 16-bits long.
Change Code to … • Mov bx, OFFSET list • Mov dl, [bx] • Int 21h • Inc bx • Mov dl, [bx] • Int 21h • Inc bx NOTE: Inc works because the data is bytes. Use: • Add bx,2 ;for word size data • Add bx,4 ;for Doubleword size data
LOOP Instruction • LOOP provides a simple way to repeat a block of statements a specific number of times. • CX (ECX) is automatically used as the counter • LOOP destination is put at the end of a section of code to be repeated. First, CX is decremented, then CX is compared to zero. • If CX is not equal to zero a jump is taken to the label identified by destination.
INCLUDE irvine16.inc .data array db 31h,32h,33h,34h ;use db to define array COUNT = ($-array) ;The $ operator gives the value of the location counter. .code main proc mov ax, @data ;copy the address of the data segment mov ds, ax ;@data into the DS register mov bx, offset array ;the offset operator returns the 16-bit offset of a label mov cx, COUNT ;set up cx register as a counter register. mov ah, 02 ;use function 2 of int 21h - display char stored in dl on screen LP1: mov dl, [bx] ;LP1 is a label int 21h inc bx loop LP1 ;decrement cx; if cx not =0,loop back to label LP1. mov ax, 4c00h int 21h main endp end main