introduction to assembly language data definition reserve words labels instruction mnemonic
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
Sahar Mosleh California State University San Marcos Page 1
Introduction to Assembly language
Data DefinitionReserve words
LabelsInstruction Mnemonic
Hello World example
Sahar Mosleh California State University San Marcos Page 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.
Sahar Mosleh California State University San Marcos Page 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 hexadecimald decimalq/o octal b binary
• Example:
26 decimal42o octal26d decimal0A3h hexadecimal11010011b binary42q Octal
Sahar Mosleh California State University San Marcos Page 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 Name Precedence level
( ) Parentheses 1
+, - Unary plus, minus 2
*, / Multiply, divide 3
Mod Modulus 3
+, - Add, subtract 4
Sahar Mosleh California State University San Marcos Page 5
• Example:
4 + 5 * 2 multiply, add12 – 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 ?
Sahar Mosleh California State University San Marcos Page 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’
Sahar Mosleh California State University San Marcos Page 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.
Sahar Mosleh California State University San Marcos Page 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
Sahar Mosleh California State University San Marcos Page 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.
Sahar Mosleh California State University San Marcos Page 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
Sahar Mosleh California State University San Marcos Page 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
Sahar Mosleh California State University San Marcos Page 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
Sahar Mosleh California State University San Marcos Page 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 expressionEax registerCount memory
Sahar Mosleh California State University San Marcos Page 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 commentthis line is also comment
&
Sahar Mosleh California State University San Marcos Page 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
Sahar Mosleh California State University San Marcos Page 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.
Sahar Mosleh California State University San Marcos Page 17
Procedure
Description
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.
Sahar Mosleh California State University San Marcos Page 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.
Sahar Mosleh California State University San Marcos Page 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) prompt1 byte “Hello World”,0 ; Store the prompt1 in
; memory .code main PROC
; (insert executable instructions here)
call clrscr ; clear the screenmov edx, offset prompt1 ; move the address of prompt to
edxcall writestring ; display the string that is stored at
;the address pointed by edx
call crlf ; move the courser to next lineexit
main ENDP; (insert additional procedure here)
END main
Sahar Mosleh California State University San Marcos Page 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
Sahar Mosleh California State University San Marcos Page 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)
call clrscr ; clear the screenmov eax, 2156543 ; move the integer to eaxcall 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
Sahar Mosleh California State University San Marcos Page 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.
Type UsageBYTE 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 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
• Value2byte 0 ; Smallest unsigned byte
• Value3byte 255 ; Largest unsigned byte
• Value4sbyte -128 ; Smallest signed byte
• Value5sbyte +127 ; Largest signed byte
Sahar Mosleh California State University San Marcos Page 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 Byte 10h• Value2 Byte 20h
Sahar Mosleh California State University San Marcos Page 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:
• word1 sword -32768 ; Smallest signed value
• word2 word 65535 ; Largest unsigned value
• word3 word ? ; un initialized, unsigned
Sahar Mosleh California State University San Marcos Page 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:
• value1 sdword -2147483648 ; Smallest signed
• value2 dword 12345678h ; unsigned value
• value3 dword ? ; un initialized, unsigned
Sahar Mosleh California State University San Marcos Page 27
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 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
var1 dword 10000var2 dword 20000Result byte “ 10000 + 20000 = “,0.code
main PROC
call clrscr ; clear the screenmov edx, offset Result ; move the address of prompt to edxcall writestringmov eax, var1 ; eax=10000add eax, var2 ; eax=10000+20000call writeint ; display the content of eax (30000)call crlf ; move the courser to next lineexit
main ENDP; (insert additional procedure here)
END main
Sahar Mosleh California State University San Marcos Page 29
SubtractionExample: 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 10000var2 dword 20000Result byte “ 10000 - 20000 = “,0.code
main PROC
call clrscr ; clear the screenmov edx, offset Result ; move the address of prompt to edxcall writestringmov eax, var1 ; eax=10000sub eax, var2 ; eax=10000-20000call writeint ; display the content of eax (-10000)call crlf ; move the courser to next lineexit
main ENDP; (insert additional procedure here)
END main
Sahar Mosleh California State University San Marcos Page 30
NegateExample: 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 27Result byte “ The negate of 27 is “,0.code
main PROC
call clrscr ; clear the screenmov edx, offset Result ; move the address of prompt to edxcall writestringmov eax, var1 ; eax=27neg eax ; eax=-27call writeint ; display the content of eax (-27)call crlf ; move the courser to next lineexit
main ENDP; (insert additional procedure here)
END main
Sahar Mosleh California State University San Marcos Page 31
INC and DECExample: 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 10000INC1 byte “ increment of 10000 is“,0DEC1 byte “ Decrement of 10000 is“,0
.code main PROC
call clrscr ; clear the screenmov edx, offset INC1 ; move the address of INC1 to edxcall writestringmov eax, var1 ; eax=10000inc eax ; eax=10001call writeint ; display the content of eax (10001)call crlf ; move the courser to next linemov edx, offset DEC1 ; move the address of DEC1 to edxcall writestringmov eax, var1 ; eax=10000dec eax ; eax=9999call writeint ; display the content of eax (9999)exit
main ENDP; (insert additional procedure here)
END main
Sahar Mosleh California State University San Marcos Page 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
Sahar Mosleh California State University San Marcos Page 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) 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 edxcall writestring ; display the string that is stored at
;the address pointed by edxcall crlf ; move the courser to next linecall Readint ;read the int from user and put it in
eaxcall writeint ;display the content of eaxexit
main ENDP; (insert additional procedure here)
END main