Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sahar Mosleh California State University San MarcosPage 1 Introduction to Assembly language Data Definition Reserve words Labels Instruction Mnemonic Hello.

Similar presentations


Presentation on theme: "Sahar Mosleh California State University San MarcosPage 1 Introduction to Assembly language Data Definition Reserve words Labels Instruction Mnemonic Hello."— Presentation transcript:

1 Sahar Mosleh California State University San MarcosPage 1 Introduction to Assembly language Data Definition Reserve words Labels Instruction Mnemonic Hello World example

2 Sahar Mosleh California State University San MarcosPage 2 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.

3 Sahar Mosleh California State University San MarcosPage 3 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 ddecimal q/ooctal bbinary Example: 26decimal 42ooctal 26ddecimal 0A3hhexadecimal 11010011bbinary 42qOctal

4 Sahar Mosleh California State University San MarcosPage 4 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). Operator NamePrecedence level ( )Parentheses1 +, -Unary plus, minus2 *, /Multiply, divide3 ModModulus3 +, -Add, subtract4

5 Sahar Mosleh California State University San MarcosPage 5 Example: 4 + 5 * 2multiply, add 12 – 1 MOD 5 modulus, subtract -5 + 2unary minus, add ( 4+2) * 6add, multiply Example: ExpressionValue -------------------------- 16 / 5? -(3 +4) * (6-1)? -3 + 4 * 6 – 1? 25 mod 3?

6 Sahar Mosleh California State University San MarcosPage 6 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’

7 Sahar Mosleh California State University San MarcosPage 7 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.

8 Sahar Mosleh California State University San MarcosPage 8 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 Var1count$first Maxopen

9 Sahar Mosleh California State University San MarcosPage 9 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.

10 Sahar Mosleh California State University San MarcosPage 10 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: Mnemonic Operand(s) ;comment

11 Sahar Mosleh California State University San MarcosPage 11 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

12 Sahar Mosleh California State University San MarcosPage 12 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: Addadd two values Subsubtract two values Mulmultiply two values Jmpjump to new location( label) Callcall a procedure Movmove (assign) one value to another

13 Sahar Mosleh California State University San MarcosPage 13 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. ExampleOperand Type ----------------------------- 96constant (immediate value) 2+4constant expression Eaxregister Countmemory

14 Sahar Mosleh California State University San MarcosPage 14 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 &

15 Sahar Mosleh California State University San MarcosPage 15 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

16 Sahar Mosleh California State University San MarcosPage 16 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.

17 Sahar Mosleh California State University San MarcosPage 17 ProcedureDescription Clrscr Crlf Delay DumpMem DumpRegs ReadChar ReadHex ReadInt ReadStrng WaitMsg WritChar WriteDec WritInt WriteString 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.

18 Sahar Mosleh California State University San MarcosPage 18 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.

19 Sahar Mosleh California State University San MarcosPage 19 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) prompt1byte “Hello World”,0 ; Store the prompt1 in ; memory.code main PROC ; (insert executable instructions here) callclrscr; clear the screen movedx, offset prompt1; move the address of prompt to edx callwritestring; display the string that is stored at ;the address pointed by edx callcrlf; move the courser to next line exit main ENDP ; (insert additional procedure here) END main

20 Sahar Mosleh California State University San MarcosPage 20 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

21 Sahar Mosleh California State University San MarcosPage 21 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) callclrscr; clear the screen moveax, 2156543; move the integer to eax callwriteInt; display the INT that is stored at eax callcrlf; move the courser to next line exit main ENDP ; (insert additional procedure here) END main

22 Sahar Mosleh California State University San MarcosPage 22 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. TypeUsage BYTE8-bit unsigned integer SBYTE8-bit signed integer WORD16-bit unsigned integer SWORD16-bit signed integer DWORD32-bit unsigned integer SDWORD32-bit signed integer FWORD48-bit integer QWORD64-bit integer TBYTE80-bit (10 byte) integer

23 Sahar Mosleh California State University San MarcosPage 23 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: Value1byte‘A’; character constant Value2byte0; Smallest unsigned byte Value3byte255; Largest unsigned byte Value4sbyte-128; Smallest signed byte Value5sbyte+127; Largest signed byte

24 Sahar Mosleh California State University San MarcosPage 24 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: Value6byte? Variable name is a label that marks the offset of variable from the beginning of it’s enclosing segment..data Value1 Byte10h Value2 Byte20h

25 Sahar Mosleh California State University San MarcosPage 25 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: word1sword-32768; Smallest signed value word2word65535; Largest unsigned value word3word?; un initialized, unsigned

26 Sahar Mosleh California State University San MarcosPage 26 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: value1sdword -2147483648; Smallest signed value2dword 12345678h; unsigned value value3dword?; un initialized, unsigned

27 Sahar Mosleh California State University San MarcosPage 27 ADD Instruction The ADD instruction adds a source operand to a destination operand of the same size. The syntax is: ADDdest, source Source is unchanged by the operation and the some is stored in the destination operand.

28 Sahar Mosleh California State University San MarcosPage 28 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 var1dword10000 var2dword20000 Resultbyte“ 10000 + 20000 = “,0.code main PROC callclrscr; clear the screen movedx, offset Result; move the address of prompt to edx callwritestring moveax, var1; eax=10000 addeax, var2; eax=10000+20000 callwriteint; display the content of eax (30000) callcrlf; move the courser to next line exit main ENDP ; (insert additional procedure here) END main

29 Sahar Mosleh California State University San MarcosPage 29 Subtraction Example: 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 var1dword10000 var2dword20000 Resultbyte“ 10000 - 20000 = “,0.code main PROC callclrscr; clear the screen movedx, offset Result; move the address of prompt to edx callwritestring moveax, var1; eax=10000 subeax, var2; eax=10000-20000 callwriteint; display the content of eax (-10000) callcrlf; move the courser to next line exit main ENDP ; (insert additional procedure here) END main

30 Sahar Mosleh California State University San MarcosPage 30 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 var1dword27 Resultbyte“ The negate of 27 is “,0.code main PROC callclrscr; clear the screen movedx, offset Result; move the address of prompt to edx callwritestring moveax, var1; eax=27 negeax ; eax=-27 callwriteint; display the content of eax (-27) callcrlf; move the courser to next line exit main ENDP ; (insert additional procedure here) END main

31 Sahar Mosleh California State University San MarcosPage 31 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 var1dword10000 INC1byte“ increment of 10000 is“,0 DEC1byte“ Decrement of 10000 is“,0.code main PROC callclrscr; clear the screen movedx, offset INC1; move the address of INC1 to edx callwritestring moveax, var1; eax=10000 inceax ; eax=10001 callwriteint; display the content of eax (10001) callcrlf; move the courser to next line movedx, offset DEC1; move the address of DEC1 to edx callwritestring moveax, var1; eax=10000 deceax ; eax=9999 callwriteint; display the content of eax (9999) exit main ENDP ; (insert additional procedure here) END main

32 Sahar Mosleh California State University San MarcosPage 32 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

33 Sahar Mosleh California State University San MarcosPage 33 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) prompt1byte “Please input an intege”,0 ; Store the prompt1 in ; memory.code main PROC movedx, offset prompt1; move the address of prompt to edx callwritestring; display the string that is stored at ;the address pointed by edx callcrlf; move the courser to next line callReadint;read the int from user and put it in eax callwriteint;display the content of eax exit main ENDP ; (insert additional procedure here) END main


Download ppt "Sahar Mosleh California State University San MarcosPage 1 Introduction to Assembly language Data Definition Reserve words Labels Instruction Mnemonic Hello."

Similar presentations


Ads by Google