1 / 41

Integer Constants

Integer Constants. Optional leading +or – sign binary, decimal, hexadecimal, or octal digits Common radix characters: h – hexadecimal d – decimal b – binary r – encoded real q/o - octal Examples: 30d, 6Ah, 42, 1101b, 33q, 22o

stuart
Télécharger la présentation

Integer Constants

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. Integer Constants • Optional leading +or – sign • binary, decimal, hexadecimal, or octal digits • Common radix characters: • h – hexadecimal • d – decimal • b – binary • r – encoded real • q/o - octal Examples: 30d, 6Ah, 42, 1101b, 33q, 22o Hexadecimal beginning with letter must have a leading zero to prevent the assembler interpreting it as an identifier : 0A5h If no radix - decimal

  2. Character and String Constants • Enclose character in single or double quotes • 'A', "x" • ASCII character = 1 byte • Enclose strings in single or double quotes • "ABC" • 'xyz' • Each character occupies a single byte • Embedded quotes: • 'Say "Goodnight," Gracie'

  3. Reserved Words • Reserved words (Appendix D) cannot be used as identifiers • Instruction mnemonics (MOVE, ADD, MUL) • Directives (INCLUDE) • type attributes (BYTE, WORD) • Operators (+, -) • predefined symbols (@data) • Directives • command understood by the assembler • not part of Intel instruction set • case insensitive

  4. Identifiers • Identifiers (programmer-chosen name) • May contain 1-247 characters, including digits • Not case-sensitive (by default) • first character must be a letter (A..Z,a..z), _, @, ? or $ • Subsequent character may also be a digit • Identifier cannot be the same as assembler reserved word. • Avoid using single @ sign as first character. • Ex : Valid identifier Var1, Count, MAX, $first, open_file, _12345, @@myfile,_main common sense – create identifier names that easy to understand

  5. Directives • Commands that are recognized / understood and acted upon by the assembler • Not part of the Intel instruction set • Used to declare code, data areas, select memory model, declare procedures, etc. • Not case sensitive (.data, .DATA, .Data - same) • Different assemblers have different directives • NASM != MASM, for example • Examples : • .DATA – identify area of a program that contains variables • .CODE – identify area of a program that contains instructions • PROC– identify beginning of a procedure. name PROC

  6. Instructions • Assembled into machine code by assembler • Executed at runtime by the CPU after the program has been loaded into memory and started. • Member of the Intel IA-32 instruction set • Parts : • A) Label (optional) • B) Mnemonic (required) • C) Operand (required) • D) Comment (optional) Figure : Standard Format for Instruction ; Comment Label : Mnemonics Operand(s) I1mov eax, 1000h ; EAX=1000h I2target : add ebx, 4000h ; taip jer apa2

  7. A) Labels • Act as place marker (penunjuk) • marks the address (offset) of code and data • Follow identifier rules (refer slide pg 8) • Data label • must be unique • example: myArray mov ax, myVariable • Code label • target of jump and loop instructions • example: L1: target: mov ax,bx … jmp target

  8. B) Mnemonics and Operands • Instruction Mnemonics is a short word that identifies the operation carried out by an instruction. (useful name) • Examples : • MOV move (assign one value to another) • ADD add two values • SUB subtract one value from another • MUL multiply two values • INC increment • DEC decrement • JMP jump to a new location • CALL call a procedure

  9. C) Operands • An assembly language can have between zero to three operands • Types of operand : • constant (immediate value) • 96, 2005h, 101011010b • constant expression • 2+4 • register • EAX, EBX, AX, AH • memory (data label) • count

  10. .. C) Operands • Examples assembly language instructions with various number of operands : • No operand stc ; set carry flag • one operand inc ax ; add 1 to AX • two operand mov count, bx ; move BX to count

  11. D) Comments • Comments are good. • explain the program's purpose • when it was written, and by whom • revision information • tricky coding techniques • application-specific explanations • Single-line comments • begin with semicolon (;) • Ex : ; add BX to AX • Multi-line comments • begin with COMMENT directive and a programmer-chosen character • end with the same programmer-chosen character • Ex : COMMENT ! This is a comment This line is also a comment Comment lagi dan lagi dan lagi…. Lepas ni habis ekk !

  12. TITLE Kira Baki Biasiswa (pokai.asm) ;Program Description: ; Author: ; Date Created: ; Last Modification Date: INCLUDE Irvine32.inc .data (insert variables here) val1 dword 10000h .code main PROC (insert executable instructions here) mov ax,@data ; initialize DS exit ; exit to operating system main ENDP (insert additional procedures here) END main

  13. Example: Adding and Subtracting Integers TITLE Add and Subtract (AddSub.asm) ; This program adds and subtracts 32-bit integers. INCLUDE Irvine32.inc .code main PROC mov eax,10000h ; EAX = 10000h add eax,40000h ; EAX = 50000h sub eax,20000h ; EAX = 30000h call DumpRegs ; display registers exit main ENDP END main Copies setup information from text file named Irvine32.inc Call procedure that displays the current values of the CPU registers. Verify program correctly

  14. mov eax,10000h 0010 0000 EAX 10000 h add eax,40000h 0050 0000 EAX 10000 h + 40000 h = 50000 h sub eax,10000h 0030 0000 EAX 50000 h – 20000 h = 30000 h

  15. Example Output Program output, showing registers and flags: EAX=00030000 EBX=7FFDF000 ECX=00000101 EDX=FFFFFFFF ESI=00000000 EDI=00000000 EBP=0012FFF0 ESP=0012FFC4 EIP=00401024 EFL=00000206 CF=0 SF=0 ZF=0 OF=0

  16. LET’S TRY • Umpukkan nilai 123400h ke dalam daftar EAX • Tambahkan kandungan EAX dengan 567800h • Tolakkan kandungan EAX dengan 777700h • Simpan EAX ke dalam variable JUMLAH (istihar sebagai WORD)

  17. Writing the program

  18. Build the program: Build 32

  19. Run the program: Run ASM

  20. Debug the program: Window Debugger

  21. Program Template TITLE Program Template (Template.asm) ; 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 procedures here) END main Instructors: please customize as needed

  22. Intrinsic Data Types (1 of 2) • BYTE, SBYTE (signed-byte) • 8-bit unsigned integer; 8-bit signed integer • WORD, SWORD • 16-bit unsigned & signed integer • DWORD, SDWORD • 32-bit unsigned & signed integer • QWORD (quadword) • 64-bit integer • TBYTE (tenbyte) • 80-bit integer

  23. Data Definition Statement • A data definition statement sets aside storage in memory for a variable. • May optionally assign a name (label) to the data • Syntax: [name] directiveinitializer [,initializer] . . . Nilai1 BYTE 34 Nilai2 BYTE ‘A’ Nilai3 BYTE 27h, 4Ch, 6Ah • All initializers become binary data in memory example : 00110010b, 32h, 50d

  24. Defining BYTE and SBYTE Data(8 bits) Each of the following defines a single byte of storage: 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 value6 BYTE ? ; uninitialized byte

  25. A variable name is a data label that implies an offset (an address). (Ilustration) Sequences of bytes Offset value data label value1 BYTE 'A' value2 BYTE 0 value3 BYTE 255 value4 SBYTE -128 value5 SBYTE +127 value6 BYTE ? value 1 value 2 value 3 value 4 value 5 value 6 0000: 0001: 0002: 0003: 0004: 0005: ‘A’ 00 FF 80 7F

  26. Defining Bytes Examples that use multiple initializers : list1 BYTE 10,20,30,40 list2 BYTE 10,20,30,40 BYTE 50,60,70,80 BYTE 81,82,83,84 list3 BYTE ?,32,41h,00100010b list4 BYTE 0Ah,20h,‘A’,22h

  27. Offset value data label (Ilustration) Sequences of bytes list1 list1+1 list1+2 list1+3 list2 list2+1 list2+2 list2+3 list2+4 list2+5 list2+6 list2+7 0000: 0001: 0002: 0003: 0004: 0005: 0006: 0007: 0008: 0009: 000A: 000B: 10 20 list1 BYTE 10,20,30,40 list2 BYTE 10,20,30,40 BYTE 50,60,70,80 BYTE 81,82,83,84 list3 BYTE ?,32,41h,00100010b list4 BYTE 0Ah,20h,‘A’,22h 30 40 10 20 30 40 50 60 70 80 :

  28. Defining Strings (1 of 2) • A string is implemented as an array of characters • For convenience, it is usually enclosed in quotation marks • It usually has a null byte at the end • Examples: str1 BYTE "Enter your name",0 str2 BYTE 'Error: halting program',0 str3 BYTE 'A','E','I','O','U' greeting1 BYTE "Welcome to the Encryption Demo program " BYTE "created by Kip Irvine.",0 greeting2 \ BYTE "Welcome to the Encryption Demo program " BYTE "created by Kip Irvine.",0

  29. Defining Strings (2 of 2) • End-of-line character sequence: • 0Dh = carriage return • 0Ah = line feed str1 BYTE "Enter your name: ",0Dh,0Ah BYTE "Enter your address: ",0 newLine BYTE 0Dh,0Ah,0 Idea: Define all strings used by your program in the same area of the data segment.

  30. Using the DUP (duplicate) Operator • Use DUP to allocate (create space for) an array or string. • Counter and argument must be constants or constant expressions var1 BYTE 20 DUP(0) ; 20 bytes, all equal to zero var2 BYTE 20 DUP(?) ; 20 bytes, uninitialized var3 BYTE 4 DUP("STACK") ; 20 bytes: "STACKSTACKSTACKSTACK" var4 BYTE 10,3 DUP(0),20 ; 0A,00,00,00,14

  31. Defining WORD and SWORD Data(2 bytes) • Define storage for 16-bit integers • or double characters • single value or multiple values word1 WORD 65535 ; largest unsigned value word2 SWORD –32768 ; smallest signed value word3 WORD ? ; uninitialized, unsigned word4 WORD "AB" ; double characters myList WORD 1,2,3,4,5 ; array of words array WORD 5 DUP(?) ; uninitialized array

  32. BYTE vs WORD cth1 BYTE 24h cth2 BYTE FFh cthA WORD 1124h cthB WORD 77FFh 0000 0001 Offset value (1) data label Offset value (2) data label cth1 cth2 cthA cthB 0000: 0001: 0002: 0003: 0004: 0005: 0000: 0002: 0004: 0006: 0008: 000A: 24 11 24 FF 77 FF

  33. WORD cthA WORD 1124h cthB WORD 77FFh 0000 0001 Offset value data label Offset value (2) data label cthA cthA+1 cthB cthB+1 cthA cthB 0000: 0001: 0002: 0003: 0004: 0005: 0000: 0002: 0004: 0006: 0008: 000A: 24 11 24 FF 77 11 FF 77

  34. Defining DWORD and SDWORD Data (4 bytes) Storage definitions for signed and unsigned 32-bit integers: val1 DWORD 12345678h ; unsigned val2 SDWORD –2147483648 ; signed val3 DWORD 20 DUP(?) ; unsigned array val4 SDWORD –3,–2,–1,0,1 ; signed array Offset value (4) data label 0000: 0004: val1 val2 12 34 56 78 80 00 00 00

  35. Defining QWORD (8 bytes) , TBYTE (10 bytes), Real Data Storage definitions for quadwords, tenbyte values, and real numbers: quad1 QWORD 1234567812345678h val1 TBYTE 1000000000123456789Ah rVal1 REAL4 -2.1 rVal2 REAL8 3.2E-260 rVal3 REAL10 4.6E+4096 ShortArray REAL4 20 DUP(0.0) Offset value (8) data label 0000: 0004: quad1 12 34 56 7812 34 56 78

  36. Bait terendah letak di address terendah Little Endian Order • All data types larger than a byte store their individual bytes in reverse order. • The least significant byte (LSB) occurs at the first (lowest) memory address. • Example: val1 DWORD 12345678h MSB LSB TRY !! Cuba1 DWORD 3740625, 2 DUP (2ABCCDEh)

  37. TRY !! cuba1 DWORD 3740625, 2 DUP (2ABCCDEh) 0000: 0001: 0002: 0003: 0004: 0005: 0006: 0007: 0008: 0009: 000A: 000B: 000C: cuba1 cuba1+1 D1 13 39 00 DE CC AB 02 DE CC AB 02

  38. TRY !! cuba2 WORD ‘B’, 25, 3 DUP (4FAh) 0000: 0001: 0002: 0003: 0004: 0005: 0006: 0007: 0008: 0009: 000A: 000B: 000C: cuba2 cuba2+1 ‘B’ 19 00 FA 04 FA 04 FA 04

  39. Adding Variables to AddSub • TITLE Add and Subtract, Version 2 (AddSub2.asm) • ; This program adds and subtracts 32-bit unsigned • ; integers and stores the sum in a variable. • INCLUDE Irvine32.inc • .data • val1 DWORD 70000h • val2 DWORD 40000h • val3 DWORD 20000h • finalVal DWORD ? • .code • main PROC • mov eax,val1 ; start with 10000h • add eax,val2 ; add 40000h • sub eax,val3 ; subtract 20000h • mov finalVal,eax ; store the result (30000h) • call DumpRegs ; display the registers • exit • main ENDP • END main

  40. Equal-Sign Directive • name = expression • expression is a 32-bit integer (expression or constant) • may be redefined • name is called a symbolic constant • good programming style to use symbols COUNT = 500 . . mov al,COUNT • Why good ? • Easy to read • Easy to maintain • if need change – only altering a • single line of code • example COUNT = 777 mov al,500

  41. EQU Directive • Define a symbol as either an integer or text expression. • Cannot be redefined PI EQU <3.1416> pressKey EQU <"Press any key to continue...",0> .data prompt BYTE pressKey

More Related