• No results found

Tutorial_Assembly Progg.pdf

N/A
N/A
Protected

Academic year: 2020

Share "Tutorial_Assembly Progg.pdf"

Copied!
18
0
0

Loading.... (view fulltext now)

Full text

(1)

SAMPLE HELLO WORLD PROGRAM

The following example shows a program that displays the traditional “Hello, world!” message on the screen. It contains the essential ingredients of an assembly language application.

TITLE Program Hello World by Andrew H. Andersen, Jr. PAGE 60,132

; Written Andrew H. Andersen, Jr. ; Brookdale Community College ; ELEC 242

; Assignment x

.MODEL SMALL

; Setup a Stack .STACK 2000H

;Define values for predefined data .DATA

prnt equ 9 done equ 4c00h

msg db ‘Hello world’ db 0Dh,0Ah,0Ah,’$’

; We could also reserve storage for use ; under program control

; This is where we state what we want done ; We begin with a Main Procedure declaration

.CODE .startup

; Now we should load the Data Segment register mov ax,@data

mov ds,ax

; This program uses an MS/DOS BIOS function ; to send a message to the screen

mov ah,prnt

lea dx,msg ;or mov dx,offset msg int 21h

; The following 2 statements return control ; back to DOS since we are finished

(2)

Here is a brief description of the important lines in the program:

♦ Line 1 contains the Title directive; all remaining characters on the line are treated as comments as are all characters on line 4. The source code for this program was written in assembly

language and must be assembled into machine language before it can run. This program is compatible with both the Microsoft and Borland assemblers.

Segments are the building blocks of programs: The code segment is where program instructions are stored; the data segment contains all variables, and the stack segment contains the program’s runtime stack. The stack is a special area of memory that the program uses when calling and returning from subroutines.

♦ The .model small directive indicates that the program uses a maximum of 64K of memory for code, and 64K of memory for data.

♦ The .stack directive sets aside 2000h (8192) bytes of stack space for the program.

♦ The .data directive indicates the beginning of the data segment where variables are stored. In the declaration of message, the assembler allocates a block of memory to hold the string containing “Hello, world!,” along with two bytes containing a ASCII sequence in MS/DOS for the newline character sequence (0Dh for Carriage Return and 0Ah for LineFeed).

The $ is a required string terminator character for the MS/DOS Function 9 console output routine being used.

♦ The .code directive indicates the beginning of the code segment. This is where the executable instructions are located.

.startup is an MASM directive that identifies the starting point of the program.

♦ The first two statements in the code segment are used to initialize the address of the data segment into the DS register.

♦ The next two statements in the code segment initialize two register for the MS/DOS Print Function. The entry requirements are

• We must load the address of a string into the DX register. DX is used as an address pointer and required by the routine.

• The MS/DOS BIOS function number is placed in the AH register. ♦ INT 21H causes DOS to execute the function

♦ The last two executable statements (beginning with the label fini) is the normal MS/DOS process end statement. This causes the program to halt and return control to the operating system.

♦ .exit is the MASM directive, which is usually near the end directive.

♦ The last line of the program contains the end directive. This is the last line of the program to be assembled. Anything placed in an asm file after the end directive will be ignored by the

assembler.

After creating the Hello World source code program with a text editor, we would save it with the name HELLO.ASM. We would then invoke the assembler with the command:

ml /Fl hello.asm

(3)

Microsoft (R) Macro Assembler Version 6.14.8444 05/29/00 08:36:36 Program Hello World by Andrew H. Andersen, Jr. Page 1 - 1

TITLE Program Hello World by Andrew H. Andersen, Jr.

PAGE 60,132

; Written Andrew H. Andersen, Jr. ; Brookdale Community College ; COMP 126 01

; Assignment x

.MODEL SMALL

; Setup a Stack .STACK 2000H

;Define values for predefined data 0000 .DATA

= 0009 prnt equ 9

= 4C00 done equ 4c00h

0000 48 65 6C 6C 6F 20 msg db ‘Hello world’ 77 6F 72 6C 64

000B 0D 0A 0A 24 db 0Dh,0Ah,0Ah,’$’

; We could also reserve storage for use ; under program control

; This is where we state what we want done ; We begin with a Main Procedure declaration

0000 .CODE .startup

; Now we should load the Data Segment register 0017 B8 ---- R mov ax,@data

001A 8E D8 mov ds,ax

; This program uses an MS/DOS BIOS function ; to send a message to the screen

001C B4 09 mov ah,prnt

001E 8D 16 0000 R lea dx,msg ;or mov dx,offset msg 0022 CD 21 int 21h

; The following 2 statements return control ; back to DOS since we are finished

0024 B8 4C00 fini: mov ax,done 0027 CD 21 int 21h

(4)

Microsoft (R) Macro Assembler Version 6.14.8444 09/16/00 11:49:32 Program Hello World by Andrew H. Andersen, Jr. Symbols 2 - 1

Segments and Groups:

N a m e Size Length Align Combine Class

DGROUP . . . GROUP

_DATA . . . 16 Bit 000F Word Public ‘DATA’ STACK . . . 16 Bit 2000 Para Stack ‘STACK’ _TEXT . . . 16 Bit 002D Word Public ‘CODE’ Microsoft (R) Macro Assembler Version 6.14.8444 09/16/00 11:49:32 Program Hello World by Andrew H. Andersen, Jr. Symbols 3 - 1

Symbols:

N a m e Type Value Attr

@CodeSize . . . Number 0000h @DataSize . . . Number 0000h @Interface . . . Number 0000h @Model . . . Number 0002h @Startup . . . L Near 0000 _TEXT @code . . . Text _TEXT

@data . . . Text DGROUP @fardata? . . . Text FAR_BSS @fardata . . . Text FAR_DATA @stack . . . Text DGROUP

done . . . Number 4C00h fini . . . L Near 0024 _TEXT msg . . . Byte 0000 _DATA

prnt . . . Number 0009h

0 Warnings 0 Errors

In C, the Hello World program source code is much smaller, and might be like the following:

/* Hello World Program

Written by Andrew H. Andersen, Jr. */

#include <stdio.h> #include <string.h>

char msg[] = “Hello world”;

main()

{

printf(“%s\n\n”,msg);

return 0

}

This program would be called HELLO.C After compiling we will have an object file.

(5)

WRITING ASSEMBLY LANGUAGE PROGRAMS

We are all aware that the English Language has specific rules that must be observed. Assembly language statements also have rules, which must be observed if we wish to have our program assembled. The source line in an assembly language statement is analogous to a sentence. The assembler recognizes three types of source lines: instructions, directives, and control. The assembly language instruction and assembler directives may consist of up to four fields as follows: NAME: OPCODE OPERAND ;COMMENT

Any number of blanks may separate the fields, but they must be separated by at least one delimiter. Each statement must appear on a single line, and is terminated with a ↵.

The maximum length of an assembly language statement is 128 characters. The characters that may be used in assembly language source statements are the letters of the alphabet, and the digits 0 through 9. Most of the other printable characters may be used, although some of these characters are reserved. In addition, any ASCII character may appear in a data string enclosed in a single quote. Both upper and lower case alphabetical characters may be used, and most assemblers treat uppercase and lowercase characters as the same.

For instance Val and val are treated as the same label or name.

When a program displays data strings enclosed in single quotes, they are printed exactly as they appear.

Certain characters have special meaning to the assembler, and these characters are called Delimiters. The delimiter defines the end of a source code statement, a field, or a component of a field. There are some delimiters that are reserved for use with Macros.

DELIMITERS

CHARACTER MEANING USE

blank 1 or more blanks field separator or terminator field ‘ ‘ single quotes enclose a data string

( ) parentheses delimit an expression

↵ RETURN statement terminator

HT TAB field separator or symbol terminator ; semicolon indicate start of a comment

: colon delimiter for labels

NAME FIELD

The name is optional, and for some assemblers a colon must follow the label in the Code segment. A label is a symbolic name used to refer to a memory address or value. The label may use any

alphanumeric character, and the first character of a label should be alphabetical. The student should use labels of six characters or less excluding the colon. This is primarily to do with spacing of lines and characters when printing the lst file. There are restrictions for label names. A label should not be the same as a mnemonic. We may not use the label SUB for a subroutine label since SUB is the assembly language mnemonic for subtract, but we could use the label SUB1. If we use a mnemonic or a single character that is the same as a register, or any reserved characters, the Assembler will flag the

(6)

OPCODE FIELD

The opcode field must always contain a valid mnemonic or assembler directive. If it does not, the assembler will flag the statement and print an error message. All operands and assembler directives should begin at least one tab space from the left margin.

OPERAND FIELD

The operand field may contain a register name, data, or a label. If there is more than one operand required by the mnemonic, it must be separated by a comma. If the operand is data, it may be in binary, octal, decimal, hex, or ASCII. The numeric data type may be upper case or lower case.

• Binary data must be entered with no spaces, has a maximum of 8, 16, or 32 bits depending on the opcode, and a B as the last character, such as 011001B.

• Octal data must be entered as a valid octal number up to 377Q for 8−bit data or 177777Q for 16−bit data with no spaces depending on the opcode, and a Q as the character, such as 175Q. • Decimal data is entered as a number up to 255 for 8−bit, 65,535 for 16−bit, or 4,294,967,296

for 32-bit unsigned data depending on the opcode. It may have no base designation or a D as the last digit of the number such a 37 or 37D.

• Hexadecimal data is entered as any valid hex number up to 0FFH for 8−bit, 0 FFFFH for 16−bit, or 0 FFFF FFFFH for 32-bit data depending on the opcode. It must have a number for the first digit and an H as the last digit of the number.

• When ASCII data is used in an operand, it must be enclosed in quotes. • Whenever the $ is present in the operand field, it means the current address.

In addition to specifying the number base in use, a valid operand could be a label with arithmetic or logical functions as part of the operand.

OPERAND ARITHMETIC OPERATIONS

OPERATION EXAMPLE EXPLANATION

+ LABEL+2 Address of LABEL + 2 bytes − LABEL−1 Address 1 byte before LABEL * TOP*3 Equates to the value TOP times 3

/ SIX/2 Equates to the value six divided by 2 or 3 NOT NOT TRUE the 1’s complement of TRUE

AND WQ AND SE returns the value of label WQ AND SE OR WQ OR SE returns the value of label WQ OR SE XOR WQ XOR SE returns the value of label WQ XOR SE

COMMENT FIELD

(7)

ASSEMBLER PSEUDO OPERATIONS

A pseudo op is an assembler directive, and is located in the opcode field. Pseudo ops give instructions to the Assembler, and it may or may not generate machine code. Labels may be used to identify pseudo ops and most require operands.

DATA ALLOCATION DIRECTIVES

A variable is a symbolic name for a location in memory where some data are stored. A variable’s offset is the distance from the beginning of the segment to the variable. A variable’s name is automatically associated with its offset. An array is a group of sequential memory locations used for storage and whose address is represented by a symbolic name. The first element of the array is at the base address. Other elements of the array are accessed by adding an offset (the number of memory locations) to the base address. For example, if we declare an array containing four characters, the name aList identifies only the offset of the first character (A):

.data

mystring db “This is a string”

mystring is a label or name. The purpose of the label is to associate a memory location in the data segment with user assigned name. This makes it easy for the programmer to access the data stored here.

The DB directive instructs the assembler to reserve storage for data on a byte boundary. T is the first letter of the string, and is placed in the base address of mystring.

The h is offset +1 or one byte after the T (the address of this memory location is mystring +1 byte). The character i is located at mystring+2 (the address of mystring plus two bytes), and so on for all characters in the string.

The address of mystring is based on its location in the program. Address Contents

mystring 54h mystring+1 68h mystring+2 69h mystring+3 73h mystring+4 20h

And so on. In the table above, the numeric data is the hex value for the ASCII character. 20h is the ASCII character for a space.

We use the following MASM data allocation directives to reserve storage on multiple byte boundaries. Different assemblers may us slightly different assembler directives. You should check the

documentation for the proper declaration syntax. For MASM:

Assember Directive Description Size DB Define byte 1 byte DW Define word 2 bytes

(8)

DB

The Define Byte directive is used to define memory storage of data on an 8−bit boundary. Labels are optional, but an operand is required. Multiple operand values may be entered in any acceptable format (numeric can be in binary, octal, decimal, or hex) and is separated by commas. Strings are enclosed in quotes.

Unsigned integer values are in the range of 0 to 255 The following are examples of the DB directive:

VALUE DB 100 ;DEFINE VALUE AS 100.

VAL2 DB 10H ;DEFINE VAL2 AS 10 IN HEX.

BINUM DB 01100111B ;DEFINE BINUM AS BINARY VALUE.

MSG DB ‘HELLO’ ;FIVE BYTE ASCII MESSAGE.

NUM DB 1,2,3,4 ;DEFINE NUM AS THE FOUR BYTES

;1, 2, 3, AND 4 IN DECIMAL.

INIT DB ? ;INITIAL VALUE UNASSIGNED

DUP The DUP operator, which is placed in the operand field, allows you to initialize storage to one or more multiple values. The DUP operator used with the DB directive is:

NUM1 DB 80 DUP (20h) ;INITIALIZES 80 BYTES OF STORAGE TO ;20H, the ASCII value for a space

VAL2 DB 5 DUP (“No”) ;DEFINE VAL2 AS NoNoNoNoNo

DW The Define Word directive is similar to the Define Byte, except that it is used to define 16−bit data (A word) instead of 8−bit data. The syntax of the DW directive is similar to DB except that it stores the defined data on a word boundary.

It is used to represent unsigned Integer values (0 to 65 535) The DW format is:

NUM1 DW 13H ;DEFINE NUM1 AS THE VALUE 0013H

NUM2 DW 1000 ;DEFINE NUM2 AS THE VALUE 1000 CAL1 DW 256 * 2 ;ASSIGN THE VALUE 512 TO CAL1 BLANK DW 80 DUP (?) ;RESERVE 80 WORDS UNINITIALIZED BLANK DW 80 DUP (20H) ;RESERVE 80 WORDS OF SPACES (0020H)

DD The Define DoubleWord directive is similar to DB and DW, except that it is used to define 32−bit data (A double word). The syntax of the DD directive is similar to DB except that it stores the defined data on a double word boundary.

It is used to represent unsigned Integer values (0 to 4 294 967 295) The DW format is:

NUM1 DD 13H ;DEFINE NUM1 AS THE VALUE 0000 0013H

DQ The Define QuadWord directive is similar to DB, except that it is used to define 64−bit data.

Unsigned integer values range from 0 to 264 – 1 (~1.8e19) The DW format is:

(9)

Equates Directive

EQU The EQU directive is used to equate a symbolic name (or label) to another label or a value. You should make liberal use of labels and equate statements since they make the program easier to understand. Although the EQU statement may appear anywhere in a program, it is a good technique to place all the equate statements together in the DATA area. An example of the equate statement is: SIX EQU 6

COUNT EQU 25

NUM EQU 100H VAL2 EQU SIX

VAL8 EQU SIX+2

Equal-Sign Directive

= This is similar to the EQU, but is used as a redefinable equates. The value associated with the = may be reassigned during assembly. Like the EQU directive, it cannot change during program execution/

Examples are: count = 25

.386

maxLongInt = 7FFF FFFFh (+2 147 486 647) minLongInt = 8000 0000h (-2 147 486 648)

maxUnsignLongInt = 0 FFFF FFFFh (4 294 967 295)

ORG The ORG directive is used to change the current address of a program to another address. It should be used in the beginning of the program, and is usually the last statement in the Equates Area. The addresses below 03FFH (4 groups of 256) are used to store the RESET and RESTART (vectored interrupt) and user defined vectored interrupt addresses. The programmer or operating system will usually load these addresses but they are not used to store source code. The ORG statement may be used at any point in the program, and may be used more than once.

It could be used to define a hex lookup table for printing the ASCII value of single hex digits that begins at a specific address.

.DATA ORG 1000H HEXTBL DB ‘01234’

DB ‘56789’

DB ‘ABCDE’

DB ‘F’

END The END assembler directive identifies the last line of the source code

(10)

OFFSET Operator

The OFFSET operator returns the offset of a memory location from the beginning of its segment. The destination operand must be a l6-bit register, as in the following example:

mov si,offset addr ; SI points to addr

SI points to addr because it contains its offset address. When the register contains an address that we wish to access,, we call it a pointer. This is a technique we will use later when we see how to access the contents of an array. In the following example, assume that byteSize is the first storage location in the data segment.

.data

byteSize db 10h,25h,3h,4Fh wordSize dw 1000h,2000h,3000h .code

mov di, offset byteSize ;DI points to address 0000 with respect to the ;base address of the data segment or DS:0000

mov bx, offset byteSize+1 ;BX points to the memory location byteSize+1 (0001) ;or DS:0001

mov si offset wordSize+2 ;SI points to the memory location DS:0006

PTR Operator

The PTR operator forces an operand to a desired boundary. For instructions where the boundary of an operand might be ambiguous, the PTR operator can be used to define the operand’s proper size in combination with the declared data size. For example, we might see:

mov al,byte ptr Num1 mov si,word ptr Num2

mov eax,dword ptr listPointer

When the size of an operand is not clear from the context of an instruction, you must make sure to use the PTR directive. Consider the following instruction, which would generate an “operand must have size” error message by the assembler:

inc [si]

In the previous instruction, the assembler cannot determine what size data SI is pointing to. By using the PTR operator, the assembler knows the data size.

inc word ptr [si]

The .MODEL is used to define the type of program being created. It automatically sets the default attribute for all procedures to either near or far.

No. of Segments OS

.model Data Code DOS Features

tiny 1 1 Used for COM only, Data and Code combined small 1 1 Smallest EXE file

medium 1 Multiple compact Multiple 1

large Multiple Multiple huge Multiple Multiple

DOS/ Windows

Uses normalized addressing

flat 1 1 Windows

NT

(11)

There are two methods to do this with MASM. The Standard Segment Directives, which works with both older and newer versions of assemblers, and the Simplified Segment Directives, which only works with MASM 5 and higher.

SEGMENT The 80x86 has different segments for the Stack, Data, and Code. Each of these three storage areas should be identified.

ENDS Defines the end of the Segment. This must occur before the END directive

STANDARD DIRECTIVE METHOD

ML assembles and links in one command. If you install MASM on your home computer and you wish to edit and assemble files from your Floppy Disk, open AUTOEXEC.BAT with EDIT in C:\ and add the following to your PATH statement:

C:\MASM611\BIN

With MASM 6.11 and higher, the ML command assemble and links a .ASM file ml hello.asm

It produces a .OBJ file and a .EXE file

Option /Fl produces a source listing with an lst extension Option /Fm produces a map file with a map extension

To produce both of these, type (with spaces) ml /Fl /Fm hello.asm

PROCESSORS

MASM supports a set of directives for selecting processors and coprocessors. Once you select a processor, you must use only the instruction set for that processor. The default is the 8086 processor. To enable a different processor modes and the additional instructions available on that processor, use the directives .186, .286, .386, and .486.

The .286P, .386P, and .486P directives enable the instructions available only at higher privilege levels in addition to the normal instruction set for the given processor. Generally, you don’t need privileged instructions unless you are writing operating-systems code or device drivers.

STACK Segment

The stack is the section of memory used for pushing or popping registers and storing the return address when a subroutine is called. The stack often holds temporary and local variables.

If your main module is written in a high-level language, that language handles the details of creating a stack. Use the .STACK directive only when you write a main module in assembly language.

The .STACK directive creates a stack segment. By default, the assembler allocates 1K of memory for the stack. This size is sufficient for most small programs.

To create a stack of a size other than the default size, give .STACK a single numeric argument indicating stack size in bytes:

.STACK 2048 ; Setup 2K stack

(12)

DATA Segment

Programs can contain both near and far data. In general, you should place important and frequently used data in the near data area, where data access is faster. This area can get crowded, however,

because in 16-bit operating systems the total amount of all near data in all modules cannot exceed 64K. Therefore, you may want to place infrequently used or particularly large data items in a far data

segment.

The .DATA, .DATA?, .CONST, .FARDATA, and .FARDATA? directives create data segments. You can access the various segments within DGROUP without reloading segment registers (see “Defining Segment Groups,” later in this chapter). These five directives also prevent instructions from appearing in data segments by assuming CS to ERROR.

Near Data Segments

The .DATA directive creates a near data segment. This segment contains the frequently used data for your program. It can occupy up to 64K in MS-DOS or 512 megabytes under flat model in Windows NT. It is placed in a special group identified as DGROUP, which is also limited to 64K.

When you use .MODEL, the assembler automatically defines DGROUP for your near data segment. The segments in DGROUP form near data, which can normally be accessed directly through DS or SS. Far Data Segments

The compact, large, and huge memory models use far data addresses by default. With these memory models, however, you can still construct data segments using .DATA, .DATA?, and .CONST. The effect of these directives does not change from one memory model to the next. They always contribute segments to the default data area, DGROUP, which has a total limit of 64K.

When you use .FARDATA or .FARDATA? in the small and medium memory models, the assembler creates far data segments FAR_DATA and FAR_BSS, respectively. You can access variables with: mov ax, SEG farvar2

mov ds, ax

Code Segment

Whether you are writing a main module or a module to be called from another module, you can have both near and far code segments. This section explains how to use near and far code segments and how to use the directives and predefined equates that relate to code segments.

Near Code Segments

The small memory model is often the best choice for assembly programs that are not linked to modules in other languages, especially if you do not need more than 64K of code. This memory model defaults to near (two-byte) addresses for code and data, which makes the program run faster and use less memory.

When you use .MODEL and simplified segment directives, the .CODE directive in your program instructs the assembler to start a code segment. The next segment directive closes the previous segment; the END directive at the end of your program closes remaining segments.

(13)

Far Code Segments

When you need more than 64K of code, use the medium, large, or huge memory model to create far segments.

The medium, large, and huge memory models use far code addresses by default. In the larger memory models, the assembler creates a different code segment for each module. If you use multiple code segments in the small, compact, or tiny model, the linker combines the .CODE segments for all modules into one segment.

For far code segments, the assembler names each code segment MODNAME_TEXT, in which MODNAME is the name of the module. With near code, the assembler names every code segment _TEXT, causing the linker to concatenate these segments into one. You can override the default name by providing an argument after .CODE. (For a complete list of segment names generated by MASM, see Appendix E, “Default Segment Names.”)

With far code, a single module can contain multiple code segments. The .CODE directive takes an optional text argument that names the segment. For instance, the following example creates two distinct code segments, FIRST_TEXT and

SECOND_TEXT.

.CODE FIRST .

. ; First set of instructions here .

.CODE SECOND .

. ; Second set of instructions here

The easiest way to begin and end an MS-DOS program is to use the .STARTUP and .EXIT directives in the main module. The main module contains the starting point and usually the termination point. You do not need these directives in a module called by another module.

These directives make MS-DOS programs easy to maintain. They automatically generate code appropriate to the stack distance specified with .MODEL. However, they do not apply to flat-model programs written for 32-bit operating systems. Thus, you should not use .STARTUP or .EXIT in programs written for Windows NT.

To start a program, place the .STARTUP directive where you want execution to begin. Usually, this location immediately follows the .CODE directive:

.CODE .STARTUP .

. ; Place executable code here .

.EXIT END

(14)

If you do not use .STARTUP, you must give the starting address as an argument to the END directive. For example, the following fragment shows how to identify a program’s starting instruction with the label start:

.CODE start: .

. ; Place executable code here .

END start

Only the END directive for the module with the starting instruction should have an argument. When .STARTUP is present, the assembler ignores any argument to END. Sometimes MASM generates a warning stating the address of end was ignored. Do not worry about this warning as it is generated because the .exit and end directives are adjacent.

(15)

SIMPLIFIED SEGMENT DIRECTIVE METHOD

Title Segment Example TOTAL.ASM by Andrew H. Andersen, Jr.

Page 60,132

;

.MODEL SMALL .DATA

ORG 7000H

POINTS DB 16 DUP(?) ;save room for 16 data bytes SUM DB ? ;save room for result (1 byte)

.CODE .STARTUP

TOTAL: MOV AX,7000H ;load address of data area MOV DS,AX ;init data segment register MOV AL,0 ;clear result

MOV BL,16 ;init loop counter LEA SI,POINTS ;init data pointer

ADDUP: ADD AL,[SI] ;add data value to result INC SI ;increment data pointer DEC BL ;decrement loop counter JNZ ADDUP ;jump if counter not zero MOV SUM,AL ;save sum

MOV AH,04CH ;Normal MS-DOS progam end

INT 21H ;Using MS-DOS

.EXIT

END TOTAL

Microsoft (R) Macro Assembler Version 6.13.7299 05/20/00 08:37:41

Segment Example TOTAL.ASM by Andrew H. Andersen, Jr. Page 1 - 1

Title Segment Example TOTAL.ASM by Andrew H. Andersen, Jr.

Page 60,132

;

.MODEL SMALL

0000 .DATA

ORG 7000H

7000 0010 [ POINTS DB 16 DUP(?) ;save room for 16 data bytes

00 ]

7010 00 SUM DB ? ;save room for result (1 byte)

0000 .CODE

.STARTUP

0017 B8 7000 TOTAL: MOV AX,7000H ;load address of data area

001A 8E D8 MOV DS,AX ;init data segment register

001C B0 00 MOV AL,0 ;clear result

001E B3 10 MOV BL,16 ;init loop counter

0020 8D 36 7000 R LEA SI,POINTS ;init data pointer

0024 02 04 ADDUP: ADD AL,[SI] ;add data value to result

0026 46 INC SI ;increment data pointer

0027 FE CB DEC BL ;decrement loop counter

0029 75 F9 JNZ ADDUP ;jump if counter not zero

002B A2 7010 R MOV SUM,AL ;save sum

002E B4 4C MOV AH,04CH ;Normal MS-DOS progam end

0030 CD 21 INT 21H ;Using MS-DOS

.EXIT

(16)

Microsoft (R) Macro Assembler Version 6.13.7299 05/20/00 08:37:41 Segment Example TOTAL.ASM by Andrew H. Andersen, Jr. Symbols 2 - 1

Segments and Groups:

N a m e Size Length Align Combine Class

DGROUP . . . GROUP

_DATA . . . 16 Bit 7011 Word Public ‘DATA’

_TEXT . . . 16 Bit 0036 Word Public ‘CODE’

Microsoft (R) Macro Assembler Version 6.13.7299 05/20/00 08:37:41

Segment Example TOTAL.ASM by Andrew H. Andersen, Jr. Symbols 3 - 1

Symbols:

N a m e Type Value Attr

@CodeSize . . . Number 0000h

@DataSize . . . Number 0000h

@Interface . . . Number 0000h

@Model . . . Number 0002h

@Startup . . . L Near 0000 _TEXT

@code . . . Text _TEXT

@data . . . Text DGROUP

@fardata? . . . Text FAR_BSS

@fardata . . . Text FAR_DATA

@stack . . . Text DGROUP

ADDUP . . . L Near 0024 _TEXT

POINTS . . . Byte 7000 _DATA

SUM . . . Byte 7010 _DATA

TOTAL . . . L Near 0017 _TEXT

1 Warnings 0 Errors

Both versions of the program accomplish the same task. The second version allows the Assembler to perform some housekeeping chores for us.

Let us briefly look at a sample line of output in the total.lst file contents and what it represents:

8000 B8 7000 TOTAL: MOV AX,7000H ;load address of data area

8000 is the Hex address in memory for this instruction B8 is the Intel HEX code for MOV AX

7000H is the hex value of the data TOTAL is the Label field

MOV is the Opcode AX,7000H is the operand ;load … is the comment.

(17)

TITLE Program STANDARD.ASM written by Andrew H. Andersen, Jr. Page 60,132

;*************************************************** ;

; Program number

; Program STANDARD.ASM

; Written by Andrew H. Andersen, Jr. ; Course - Section

;

; This program , ;

; We use the procedures Clrscr, Gotoxy, Writestring ; Readint, and Writeint from LIBRARY.LIB

;

;***************************************************

.model small .stack 1000h

.data

row1 equ 0516h row2 equ 0616h row3 equ 0916h row4 equ 0a16h row5 equ 0b20h

prompt1 db ‘This program adds two decimal numbers’,0 prompt2 db ‘that you enter from the keyboard.’,0 get1st db ‘Enter the 1st number --> ‘,0

get2nd db ‘Enter the 2nd number --> ‘,0 showans db ‘The sum is --> ‘,0

.code

; Define proc used from library.lib ; the \ is continue form previous line

extern Writeint:proc, Writestring:proc, \ Readint:proc, Gotoxy:proc,

.startup

(18)

;************ ;

; Define what happens here ;

;************

init: call Clr call SetCur

; the rest of main is placed here

fini: mov ah,04ch ;standard MS/DOS Process End int 21h

; ALL PROCEDURES ARE HERE

; Clear the screen using MS/DOS Video BIOS

Clr proc

; Clear Screen with 25 line scroll.

; The screen is blue and the text is yellow

mov ah, 6 ;The Video BIOS function mov al, 0 ;Scroll # of lines 0 = all mov cx, 0 ;row/col at top

mov dx, 1950h ;row/col at bottom 24 & 79 mov bh, 1eh ;foreground/background colors int 10h

Clr endp

SetCur proc

; Now set Cursor to row 5 column 30 on page 0 mov ah, 2 ;The BIOS function

mov dx, 051eh ;row/column position mov bh, 0 ;video page no. 0 int 10h ;using Video BIOS ret

SetCur endp

.exit

References

Related documents

Despite the observation that gB/CPG/ alum was able to induce a higher T cell response than gD/ CPG/alum (and an equivalent neutralizing antibody response), the level of

This gives rise to a potential security concern for the Cloud users because it makes private data of one client vulnerable to theft by other competing clients who run

Rackspace supports integration with the other components of OpenStack, as well as features such as floating IP address management, security groups, availability zones, and

The performance indicators measured the impact of the mobile technology on the efficiency of the stocking process, the error rates, the process flexibility, the number of work

Students received the EBM course delivered using either a didactic approach (DID) to learning EBM or a blended-learning approach (BL).. Student competency in EBM was assessed using

Other things equal, if women are over- represented amongst the low paid, and the proportion of low paid workers varies across regions, we might expect to see the introduction of the

We introduced an algorithm of text-based LSTM networks for automatic com- position and reported results for generating chord progressions and rock drum tracks. Word-RNNs showed

Enzyme synthesis was affected by pH,temperatures &amp; incubation period.The optimum temperature enzyme activity was found at 37°C, whereas maximum enzyme activity