1 / 33

Introduction to Assembly language Data Definition Reserve words Labels Instruction Mnemonic

Introduction to Assembly language Data Definition Reserve words Labels Instruction Mnemonic Hello World example. Data Definitions We have covered some essential basic of computer hardware as well as specific knowledge of the IA-32 architecture

alpha
Download Presentation

Introduction to Assembly language Data Definition Reserve words Labels Instruction Mnemonic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Assembly language Data Definition Reserve words Labels Instruction Mnemonic Hello World example

  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. 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 decimal • 42o octal • 26d decimal • 0A3h hexadecimal • 11010011b binary • 42q Octal

  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).

  5. Example: • 4 + 5 * 2 multiply, add • 12 – 1 MOD 5 modulus, subtract • -5 + 2 unary minus, add • ( 4+2) * 6 add, multiply • Example: • Expression Value • ----------------- --------- • 16 / 5 ? • -(3 +4) * (6-1) ? • -3 + 4 * 6 – 1 ? • 25 mod 3 ?

  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. 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. 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 count $first • Max open

  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. 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. 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. 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 add two values • Sub subtract two values • Mul multiply two values • Jmp jump to new location( label) • Call call a procedure • Mov move (assign) one value to another

  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. • Example Operand Type • ----------- ------------------ • 96 constant (immediate value) • 2+4 constant expression • Eax register • Count memory

  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. 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. 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. 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.

  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 clrscr ; clear the screen 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 exit main ENDP ; (insert additional procedure here) END main

  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

  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 clrscr ; clear the screen mov eax, 2156543 ; move the integer to eax call writeInt ; display the INT that is stored at eax call crlf ; move the courser to next line exit main ENDP ; (insert additional procedure here) END main

  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.

  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 byte ‘A’ ; character constant • Value2 byte 0 ; Smallest unsigned byte • Value3 byte 255 ; Largest unsigned byte • Value4 sbyte -128 ; Smallest signed byte • Value5 sbyte +127 ; Largest signed byte

  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 Byte 10h • Value2 Byte 20h

  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 sword -32768 ; Smallest signed value • word2 word 65535 ; Largest unsigned value • word3 word ? ; un initialized, unsigned

  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 dword 12345678h ; unsigned value • value3 dword ? ; un initialized, unsigned

  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.

  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 ; clear the screen mov edx, offset Result ; move the address of prompt to edx call writestring mov eax, var1 ; eax=10000 add eax, var2 ; eax=10000+20000 call writeint ; display the content of eax (30000) call crlf ; move the courser to next line exit main ENDP ; (insert additional procedure here) END main

  28. 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 var1 dword 10000 var2 dword 20000 Result byte “ 10000 - 20000 = “,0 .code main PROC call clrscr ; clear the screen mov edx, offset Result ; move the address of prompt to edx call writestring mov eax, var1 ; eax=10000 sub eax, var2 ; eax=10000-20000 call writeint ; display the content of eax (-10000) call crlf ; move the courser to next line exit main ENDP ; (insert additional procedure here) END main

  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 ; clear the screen mov edx, offset Result ; move the address of prompt to edx call writestring mov eax, var1 ; eax=27 neg eax ; eax=-27 call writeint ; display the content of eax (-27) call crlf ; move the courser to next line exit main ENDP ; (insert additional procedure here) END main

  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 ; clear the screen mov edx, offset INC1 ; move the address of INC1 to edx call writestring mov eax, var1 ; eax=10000 inc eax ; eax=10001 call writeint ; display the content of eax (10001) call crlf ; move the courser to next line mov edx, offset DEC1 ; move the address of DEC1 to edx call writestring mov eax, var1 ; eax=10000 dec eax ; eax=9999 call writeint ; display the content of eax (9999) exit main ENDP ; (insert additional procedure here) END main

  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

  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

More Related