Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Introduction to Assembly language Data Definition Reserve words Labels Instruction Mnemonic Hello World example Sahar Mosleh California State University San Marcos Page 1 Data Definitions • We have covered some essential basic of computer hardware as well as specific knowledge of the IA-32 architecture • Assembly language programmer absolutely must know their data backwards and forwards before writing executable code. • In this chapter you will learn about how to define and declare variables and constants, using Microsoft Assembler (MASM) syntax. Sahar Mosleh California State University San Marcos Page 2 Constant • An integer constant is made up of an optional leading sign, one or more digits, and an optional suffix character indicating the number’s base: • The radix may be one of the following (uppercase or lowercase): h hexadecimal d decimal q/o octal b binary • Example: 26 42o 26d 0A3h 11010011b 42q Sahar Mosleh decimal octal decimal hexadecimal binary Octal California State University San Marcos Page 3 Integer expression • An integer expression is a mathematical expression involving integer values and arithmetic operators. • The arithmetic operator are listed in below according to their precedence order from highest(1) to lowest(4). Sahar Mosleh Operator Name Precedence level () Parentheses 1 +, - Unary plus, minus 2 *, / Multiply, divide 3 Mod Modulus 3 +, - Add, subtract 4 California State University San Marcos Page 4 • Example: 4+5*2 12 – 1 MOD 5 -5 + 2 ( 4+2) * 6 multiply, add modulus, subtract unary minus, add add, multiply • Example: Expression ----------------16 / 5 -(3 +4) * (6-1) -3 + 4 * 6 – 1 25 mod 3 Sahar Mosleh Value --------? ? ? ? California State University San Marcos Page 5 Character and string constants • Character constant is a single character enclosed in either single or double quotes. the assembler converts it to binary ASCII code matching the character. • Example: ‘A’ ‘d’ • String constant is a string of characters enclosed in ether single or double quotes: Example: ‘ABC’ ‘This is a test’ Sahar Mosleh California State University San Marcos Page 6 Reserved Words • Assembly language has a list of words called reserved words. These have special meaning and can be used in their correct context. Reserved words can be any of the following: • Instruction mnemonics, such as MOV, ADD, or MUL, which correspond to built-in operations preformed by Intel processor, • Directives, which tell MAZM how to assemble programs. • Attributes, which provide size and usage information for variables and operands. Examples are BYTE and WORD. • Operators, used in constant expressions. • A complete list of MAZM reserved words will be found in Appendix D. Sahar Mosleh California State University San Marcos Page 7 Identifiers • An identifier is a programmer chosen name. it might identify a variable, a constant, a procedure, or a code label. Keep the following in mind when creating identifiers: • They may contain between 1 and 247 characters. • They are not case sensitive. • The first character must be either a letter (A…Z, a…z ), underscore(_), $. Subsequent character may also be digits. • An identifier can not be the same as an assembler reserved word. • Example of identifiers • Var1 Max Sahar Mosleh count open $first California State University San Marcos Page 8 Directive • A directive is a command that is recognized and acted upon by the assembler as the program's source code is being assembled. • Directives are being used for defining logical segments, choosing a model, defining variables, creating procedures, and so on. • Different capitalization of the same directive are assumed to be equivalent. For example the assembler does not recognize any difference between .data, .DATA, and .Data. • Examples of directives are: • .Data ;identifies the area of a program that contains variables; • .code ; identifies the area of a program that contains instructions; • A-name proc ;identifies the beginning of procedures; • It would take a very long time to learn all the directives in MAZM, so we concentrate on the few that are most essential. Sahar Mosleh California State University San Marcos Page 9 Instructions • An instruction is a statement that is executed by the processor at runtime after the program has been loaded into memory and started. • An instruction contain four basic parts: • Label (option) • Instruction mnemonic (required) • Operand(s) (usually required) • Comments (optional) • The following diagram shows the standard format for instructions. Label: Sahar Mosleh Mnemonic Operand(s) California State University San Marcos ;comment Page 10 label • A lable is an identifier that acts as a place marker for either instructions (code) or data • Example of code label: target: mov eax, ebx ….. jmp target • Example of data label first byte 10 Remember there is no colon after first Sahar Mosleh California State University San Marcos Page 11 Instruction mnemonic • An instruction mnemonic ( in English dictionary described as a device that assist memory) is a short word that identifies the operation carried out by an instruction. • Examples: • Add • Sub • Mul • Jmp • Call • Mov Sahar Mosleh add two values subtract two values multiply two values jump to new location( label) call a procedure move (assign) one value to another California State University San Marcos Page 12 Operands • Assembly language instruction can have between zero and three operands, each of which can be a register, memory operand, constant expression, or I/O port. • We will discuss the different type of operand later. Example ----------96 2+4 Eax Count Sahar Mosleh Operand Type -----------------constant (immediate value) constant expression register memory California State University San Marcos Page 13 Comments • Comments as you probably know, are an important way for the writer of a program to communicate information about how the program works to the person reading the source code. • Comments can be specified by semicolon. • Example: • ;This line is comment; • For Block comments you can use COMMENT directive and a user specified symbol. • Example: COMMENT & This line is comment this line is also comment & Sahar Mosleh California State University San Marcos Page 14 Program Template TITEL Program Template ; Program Description: ; Author: ; Creation Date: ; Revisions: ; Date: Modified by: INCLUDE Irvine32.inc .data ; (insert variables here) .code main PROC ; (insert executable instructions here) exit main ENDP ; (insert additional procedure here) END main Sahar Mosleh California State University San Marcos Page 15 Irvine32 Library • Irvine32 library contains useful procedures that have been written in assembly and by including them to your program, you can call them. • The table presented in next slide references some of these procedures. Sahar Mosleh California State University San Marcos Page 16 Procedure Clrscr Crlf Delay DumpMem DumpRegs ReadChar ReadHex ReadInt ReadStrng WaitMsg WritChar WriteDec WritInt WriteString Sahar Mosleh Description Clear the console and locates at the upper left corner Writes an end of the line sequence to standard output Pauses the program execution for a specified n millisecond interval Writes a block of memory to standard output in hexadecimal. Displays the EAX, EBX, ECX, EDX, ESI, EDI, EBP, ESP, EFLAGS, and EIP registers in hex. Also display the Carry, Sign, Zero, and Overflow flags. Read a single character from standard input Reads a 32-bit hexadecimal integer from input, terminated by the Enter Key Read a 32-bit signed decimal integer from standard input, terminated by the Enter key. Reads a string from standard input, terminated by Enter Key. Displays a message and waits for the enter Key to be pressed. Writes a single character to standard output. Writes an unsigned 32-bit integer to standard output in hex format Writes a signed 32-bit integer to standard output in decimal format. Writes a null terminated string to standard output. California State University San Marcos Page 17 detailed description of the procedures • WriteString procedure writes a null terminated string to standard output. When calling it, place the string’s offset in EDX. • Clrscr procedure clears the screen. This is typically done at the beginning and the ending of a program. If you call it at other times during a program’s execution, remember to pause the program (by calling WaitMsg ) before calling Clscr. This the will allow user to view the information already on the screen before it is erased. • Crlf procedure advance the courser to the beginning of the next line of standard output. • The example on the next slides puts all these information together to produce “Hello World” Message on the screen. Sahar Mosleh California State University San Marcos Page 18 Example: TITLE Hello Program ; Program Description: This program will display Hello World message on the ; screen ; Author: Sahar Mosleh ; Creation Date: August 30, 2005 INCLUDE Irvine32.inc .data ; (insert variables here) prompt1 byte “Hello World”,0 ; Store the prompt1 in ; memory .code main PROC ; (insert executable instructions here) call mov call clrscr edx, offset prompt1 writestring call crlf exit main ENDP ; (insert additional procedure here) END main Sahar Mosleh ; clear the screen ; move the address of prompt to edx ; display the string that is stored at ;the address pointed by edx ; move the courser to next line California State University San Marcos Page 19 More on library procedures • WriteInt procedure write a 32 bit signed integer to standard output in decimal format with a leading sign and no leading Zeros. The integer needs to be placed in EAX first and then call WriteInt procedure. • WaitMsg displays the message “press [Enter] to continue..” and waits for the user to press the Enter key. This is useful when you want to pause the screen display before data scrolls off and disappears. • Example on next slide Sahar Mosleh California State University San Marcos Page 20 Example: TITLE Print Integer ; Program Description: This program will display integer number 216543 on the ; screen ; Author: Sahar Mosleh ; Creation Date: August 30, 2005 INCLUDE Irvine32.inc .data ; (insert variables here) .code main PROC ; (insert executable instructions here) call mov call call clrscr eax, 2156543 writeInt crlf ; clear the screen ; move the integer to eax ; display the INT that is stored at eax ; move the courser to next line exit main ENDP ; (insert additional procedure here) END main Sahar Mosleh California State University San Marcos Page 21 Defining Data • MASM defines various data types, each of which describes a set of values that can be assigned to variables and expressions of the given type. • The Various integer data type is been defined in the fallowing table. Type Usage BYTE 8-bit unsigned integer SBYTE 8-bit signed integer WORD 16-bit unsigned integer SWORD 16-bit signed integer DWORD 32-bit unsigned integer SDWORD 32-bit signed integer FWORD 48-bit integer QWORD 64-bit integer TBYTE 80-bit (10 byte) integer Sahar Mosleh California State University San Marcos Page 22 Defining Byte and SBYTE Data • The BYTE ( define byte) and SBYTE ( signed byte ) directives, used in data definition statements, allocate storage for one or more 8 bits signed or unsigned values. • Each initializer must be an 8-bit integer or character constant. • Example: • Value1 • Value2 • Value3 • Value4 byte byte byte sbyte • Value5 sbyte +127 ; Largest signed byte Sahar Mosleh ‘A’ 0 255 -128 ; character constant ; Smallest unsigned byte ; Largest unsigned byte ; Smallest signed byte California State University San Marcos Page 23 Variables • A variable can be left un initialized by using a question mark for the initializer. • This implies that the variable will be assigned a value at runtime by executable instructions. • Example: • Value6 byte ? • Variable name is a label that marks the offset of variable from the beginning of it’s enclosing segment. • .data • Value1 • Value2 Sahar Mosleh Byte Byte 10h 20h California State University San Marcos Page 24 Defining WORD and SWORD Data • The WORD ( define word) and SWORD ( signed word ) directives, used in data definition statements, allocate storage for one or more 16- bit unsigned or signed integer. . • Example: • word1 • word2 • word3 Sahar Mosleh sword -32768 word 65535 word ? ; Smallest signed value ; Largest unsigned value ; un initialized, unsigned California State University San Marcos Page 25 Defining DWORD and SDWORD Data • The DWORD ( define double word) and SDWORD ( signed double word ) directives, used in data definition statements, allocate storage for one or more 32- bit unsigned or signed integer. . • Example: • value1 sdword -2147483648 ; Smallest signed • value2 • value3 dword dword 12345678h ? ; unsigned value ; un initialized, unsigned Sahar Mosleh California State University San Marcos Page 26 ADD Instruction • The ADD instruction adds a source operand to a destination operand of the same size. The syntax is: •ADD dest, source • Source is unchanged by the operation and the some is stored in the destination operand. Sahar Mosleh California State University San Marcos Page 27 Example: TITLE Addition ; Program Description: This program will add two 32-bit number together and output the result ; Author: Sahar Mosleh ; Creation Date: August 30, 2005 INCLUDE Irvine32.inc .data var1 dword 10000 var2 dword 20000 Result byte “ 10000 + 20000 = “,0 .code main PROC call clrscr mov edx, offset Result call writestring mov eax, var1 add eax, var2 call writeint call crlf exit main ENDP ; (insert additional procedure here) END main Sahar Mosleh ; clear the screen ; move the address of prompt to edx ; eax=10000 ; eax=10000+20000 ; display the content of eax (30000) ; move the courser to next line California State University San Marcos Page 28 Example: Subtraction TITLE Subtraction ; Program Description: This program will subtract two 32-bit number together and output the result ; Author: Sahar Mosleh ; Creation Date: August 30, 2005 INCLUDE Irvine32.inc .data var1 dword 10000 var2 dword 20000 Result byte “ 10000 - 20000 = “,0 .code main PROC call clrscr mov edx, offset Result call writestring mov eax, var1 sub eax, var2 call writeint call crlf exit main ENDP ; (insert additional procedure here) END main Sahar Mosleh ; clear the screen ; move the address of prompt to edx ; eax=10000 ; eax=10000-20000 ; display the content of eax (-10000) ; move the courser to next line California State University San Marcos Page 29 Negate Example: TITLE Negate ; Program Description: This program will negate the number 27 ; Author: Sahar Mosleh ; Creation Date: August 30, 2005 INCLUDE Irvine32.inc .data var1 dword 27 Result byte “ The negate of 27 is “,0 .code main PROC call clrscr mov edx, offset Result call writestring mov eax, var1 neg eax call writeint call crlf exit main ENDP ; (insert additional procedure here) END main Sahar Mosleh ; clear the screen ; move the address of prompt to edx ; eax=27 ; eax=-27 ; display the content of eax (-27) ; move the courser to next line California State University San Marcos Page 30 INC and DEC Example: TITLE Subtraction ; Program Description: This program will increment and decrement value of 10000. ; Author: Sahar Mosleh ; Creation Date: August 30, 2005 INCLUDE Irvine32.inc .data var1 dword 10000 INC1 byte “ increment of 10000 is“,0 DEC1 byte “ Decrement of 10000 is“,0 .code main PROC call clrscr mov edx, offset INC1 call writestring mov eax, var1 inc eax call writeint call crlf mov edx, offset DEC1 call writestring mov eax, var1 dec eax call writeint exit main ENDP ; (insert additional procedure here) END main Sahar Mosleh ; clear the screen ; move the address of INC1 to edx ; eax=10000 ; eax=10001 ; display the content of eax (10001) ; move the courser to next line ; move the address of DEC1 to edx ; eax=10000 ; eax=9999 ; display the content of eax (9999) California State University San Marcos Page 31 Read Integer procedure • ReadInt procedure reads a 32bit signed integer from standard input and return the value in EAX. The user can type an optional leading plus or minus singe, and the rest of the integer number can only consist of the digits. • ReadInt will set the over flow flag and display an error message if the value entered can not be represented as a 32-bit signed integer • See example on next slide Sahar Mosleh California State University San Marcos Page 32 Example: TITLE Input Integer ; Program Description: This program will read integer from user and out put ;it on the screen ; Author: Sahar Mosleh ; Creation Date: August 30, 2005 INCLUDE Irvine32.inc .data ; (insert variables here) prompt1 byte “Please input an intege”,0 ; Store the prompt1 in ; memory .code main PROC mov edx, offset prompt1 ; move the address of prompt to edx call writestring ; display the string that is stored at ;the address pointed by edx call crlf ; move the courser to next line call Readint ;read the int from user and put it in eax call writeint ;display the content of eax exit main ENDP ; (insert additional procedure here) END main Sahar Mosleh California State University San Marcos Page 33