You are on page 1of 11

Wendy Zhang, Computer Science & IT Department

Chapter 3: Assemb y !anguage "undamenta s I# $b%ecti&es


A'ter reading this Chapter, you shou d be ab e to understand or do each o' the 'o o(ing: )no( ho( to represent integer constants, e*pressions, rea number constants, character constants, and string constants in assemb y anguage )no( ho( to 'ormu ate assemb y anguage instructions, using &a id synta* +nderstand the di''erence bet(een instructions and directi&es ,e ab e to code, assemb e, and e*ecute a program that adds and subtracts integers ,e ab e to create &ariab es using a standard assemb y anguage data types ,e ab e to de'ine symbo ic constants ,e ab e to ca cu ate the si-e o' arrays at assemb y time

II# !ecture .otes


1. Basic Elements of Assembly Language Microsoft syntax notation o / ements (ithin s0uare brac1ets 2 3 are optiona o / ements (ithin 4 56 5657 re0uires a choice o' the enc osed e ements o / ements in italics denote items (hich ha&e 1no(n de'initions or descriptions o /*amp e: 248 6 973 digits 2radix3 Integer constants (integer literal) o Synta*: 248 6 973 digits 2radix3 o $ptiona eading 8 or : sign o binary, decima , he*adecima , or octa digits o Common radix characters: h hexadecimal d decimal b binary r encoded real o /*amp es: 3;d, <Ah, =>, ??;?b o @e*adecima beginning (ith etter must ha&e eading ;: ;AAh o I' no radi* is gi&en, the integer constant is assumed to be decima Integer expressions o A mathematica e*pression in&o &ing integer value and arithmetic operators o $perators and precedence e&e s:

Assemb y !anguage 'or Inte 9,ased Computers

Chapter 3

Wendy Zhang, Computer Science & IT Department

o /*amp es: ?> : ? B$D A, 93 8 = C< 9 ? Real Number Constants o Synta*: 2sign3 integer#2integer32exponent3 sign 48 6 97 exponent /248 6 973integer o /*amp es: >#, 83#;, 9==#></8;A, ><#/9A o Note: At the &ery east, there must be a digita or a decima point Character and String Constants o /nc ose character in sing e or doub e 0uotes DAD, E*E ASCII character F ? byte o /nc ose strings in sing e or doub e 0uotes EA,CE D*y-D /ach character occupies a sing e byte o /mbedded 0uotes: DSay EGoodnight,E GracieD Reserved Words and Identifiers o Reserved words cannot be used as identi'iers Instruction mnemonics, directives, type attributes, operators, predefined symbols See BASB re'erence in Appendi* A o Identifiers : a programmer9choice name ?9>=H characters, inc uding digits not case sensiti&e 'irst character must be a etter, I, J, K, or L irectives o Commands that are recogni-ed and acted upon by the assemb er .ot part o' the Inte instruction set +sed to dec are code, data areas, se ect memory mode , dec are procedures, etc# not case sensiti&e o Di''erent assemb ers ha&e di''erent directi&es .ASB not the same as BASB See BASB re'erence in Appendi* D Instructions o Assemb ed into machine code by assemb er o /*ecuted at runtime by the CM+ o We use the Inte IA93> instruction set o An instruction contains: !abels Noptiona O Act as p ace mar1ers mar1s the address No''setO o' code and data "o o( identi'er ru es Data abe

Assemb y !anguage 'or Inte 9,ased Computers

>

Chapter 3

Wendy Zhang, Computer Science & IT Department

9 must be uni0ue 9 e*amp e: myArray Nnot 'o o(ed by co onO Code abe 9 target o' %ump and oop instructions 9 e*amp e: L1: N'o o(ed by co onO Mnemonics Nre0uiredO Instruction Bnemonics memory aid e*amp es: B$P, ADD, S+,, B+!, I.C, D/C "perands Ndepends on the instructionO constant constant e*pression register memory Ndata abe O Constants and constant e*pressions are o'ten ca ed immediate &a ues Comments Noptiona O Comments are goodQ 9 e*p ain the programDs purpose 9 (hen it (as (ritten, and by (hom 9 re&ision in'ormation 9 tric1y coding techni0ues 9 app ication9speci'ic e*p anations Sing e9 ine comments 9 begin (ith semico on NRO Bu ti9 ine comments 9 begin (ith C$BB/.T directi&e and a programmer9chosen character 9 end (ith the same programmer9chosen character 9 /*amp e: C$BB/.T & This is a comment# This ine is a so a comment# & o Instruction "ormat /*amp es .o operands
stc ; set Carry flag ; register ; memory ; register, register ; memory, constant ; register, constant-expression

$ne operand
inc eax inc myByte

T(o operands
add ebx,ecx sub myByte,25 add eax,36 * 25

Assemb y !anguage 'or Inte 9,ased Computers

Chapter 3

Wendy Zhang, Computer Science & IT Department

2. Example: Adding and Subtracting Integers #rogram listing


TITLE Add and Subtract (AddSub.asm) ; This program adds and subtracts 32-bit integers. IN L!"E Ir#ine32.inc .code main $%& mo# add sub ca// e'it main EN"$ EN" main ea'()****h ea'(-****h ea'(2****h "ump%egs ; EA+ , )****h ; EA+ , .****h ; EA+ , 3****h

#rogram "utput: sho(ing registers and ' ags:


EAX=00030000 ESI=00000000 EIP=00401024 EBX=7FFDF000 EDI=00000000 EFL=00000206 ECX=00000101 EDX=FFFFFFFF EBP=0012FFF0 ESP=0012FFC4 CF=0 SF=0 ZF=0 OF=0

#rogram escription o The I LE directi&e mar1s the entire ine as a comment o The IN!L"#E directi&e copies necessary de'initions and setup in'ormation 'rom a test 'i e NIrvine32.incO ocated in assemb erSs I.C!+D/ directory o The .code directi&e mar1s the beginning o' the code segment o The $%&! directi&e identi'ies the beginning o' a procedure o The '&(E instruction mo&es NcopiesO the second operand Nsource operandO to the 'irst operand Ndestination operatorO o The A## instruction add second operand to the 'irst operand o The S"B instruction subtracts second operand 'rom the 'rom operand o The !ALL statement ca s a procedure o The exit statement ca s a prede'ined BS9Windo( 'unction that ha ts the program o The EN#$ directi&e mar1s the end o' the procedure o The EN# directi&e mar1s the ast ine o' the program to be assemb ed Segments $ organi-e the program o The code segment ).codeO contains a o' the programSs e*ecutab e instruction o The data segment N#dataO ho ds &ariab e o The stac1 N#stac1O ho ds procedure parameters and oca &ariab es Suggested Coding Standards o capita i-e on y directi&es and operators o descripti&e identi'ier names o spaces surrounding arithmetic operators o code and data abe s : no indentation o e*ecutab e instructions : indent =9A spaces o comments: begin at co umn =;9=A, a igned &ertica y

Assemb y !anguage 'or Inte 9,ased Computers

Chapter 3

Wendy Zhang, Computer Science & IT Department

o ?93 spaces bet(een instruction and its operands *: mo& a*,b* o ?9> b an1 ines bet(een procedures #rogram %emplate
TITLE $rogram Temp/ate ; $rogram "escription0 ;Author0 ; "ate reated0 ; Last 1odi2ication "ate0 IN L!"E Ir#ine32.inc ; (insert s3mbo/ de2initions here) .data ; (insert #ariab/es here) .code main $%& ; (insert e'ecutab/e instructions here) e'it main EN"$ ; e'it to operating s3stem (temp/ate.asm)

; (insert additiona/ procedures here) EN" main

Assemb y !anguage 'or Inte 9,ased Computers

Chapter 3

Wendy Zhang, Computer Science & IT Department

*. Assembling+ Lin,ing+ and %unning $rograms &ssemble'!in( )xecute Cycle Link Library
Source File
Step 2: assembler

Object File Listing File

Step 3: linker

Executable File Map File

Step 4: OS loader

Output

Step 1: text editor

I' the source code is modi'ied, Steps > through = must be repeated# !isting *ile o +se it to see ho( your program is compi ed o Contains source code addresses ob%ect code Nmachine anguageO segment names symbo s N&ariab es, procedures, and constantsO o /*amp e: addSub# st Bap "i e o In'ormation about each program segment: starting address ending address si-e segment type o /*amp e: addSub#map N?<9bit &ersionO

Assemb y !anguage 'or Inte 9,ased Computers

<

Chapter 3

Wendy Zhang, Computer Science & IT Department

-. #efining #ata Intrinsic ata %ypes o B !", #B !" T9bit unsigned integerR T9bit signed integer o $%R&, #$%R& ?<9bit unsigned & signed integer o &$%R&, #&$%R& 3>9bit unsigned & signed integer o '$%R& <=9bit integer o !B !" T;9bit integer o R"()* =9byte I/// short rea o R"()+ T9byte I/// ong rea o R"(),?;9byte I/// e*tended rea ata efinition Statement o A data de'inition statement sets aside storage in memory 'or a &ariab e# o Bay optiona y assign a name N abe O to the data o Synta*: 2name3 directive initiali.er 2,initiali.er3 # # # o /*amp e:
value1 BYTE 10

o A initia i-ers become binary data in memory efining +,%) and S+,%) ata
value1 value2 value3 value4 value5 value6 BYTE 'A' BYTE 0 BYTE 255 SBYTE -128 SBYTE +127 BYTE ? ; ; ; ; ; ; character constant smallest unsigned byte largest unsigned byte smallest signed byte largest signed byte uninitialized byte

o BASB does not pre&ent you 'rom initia i-ing a ,UT/ (ith a negati&e &a ue, but itDs considered poor sty e# o I' you dec are a S,UT/ &ariab e, the Bicroso't debugger (i automatica y disp ay its &a ue in decima (ith a eading sign# efining +yte &rrays ' use mu tip e initia i-ers
list1 BYTE 10,20,30,40
Offset 0000 0001 0002 0003 Value 10 20 30 40

list2 BYTE 10,20,30,40 BYTE 50,60,70,80 BYTE 81,82,83,84

Assemb y !anguage 'or Inte 9,ased Computers

Chapter 3

Wendy Zhang, Computer Science & IT Department

list3 BYTE ?,32,41h,00100010b list4 BYTE 0Ah,20h,A,22h

efining Strings o A string is imp emented as an array o' characters o "or con&enience, it is usua y enc osed in 0uotation mar1s o It o'ten (i be nu 9terminated o /*amp es:
str1 BYTE "Enter your name",0 str2 BYTE 'Error: halting program',0 str3 BYTE 'A','E','I','O','U' greeting BYTE "Welcome to the Encryption Demo program " BYTE "created by Kip Irvine.",0

o comma:

To continue a sing e string across mu tip e ines, end each ine (ith a
menu BYTE "Checking Account",0dh,0ah,0dh,0ah, "1. Create a new account",0dh,0ah, "2. Open an existing account",0dh,0ah, "3. Credit the account",0dh,0ah, "4. Debit the account",0dh,0ah, "5. Exit",0ah,0ah, "Choice> ",0

"nd/of/line character se0uence1 ;Dh F carriage return ;Ah F ine 'eed o De'ine a strings used by your program in the same area o' the data segment -sing the -# "perator o +se D+M to a ocate Ncreate space 'orO an array or string# o Synta*: counter D+M N argument O o 2ounter and argument must be constants or constant e*pressions o /*amp es: o
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"

efining W"R and SW"R ata De'ine storage 'or ?<9bit integers or doub e characters, sing e &a ue or mu tip e &a ues
word1 word2 word3 word4 myList array WORD SWORD WORD WORD WORD WORD 65535 32768 ? "AB" 1,2,3,4,5 5 DUP(?) ; ; ; ; ; ; largest unsigned value smallest signed value uninitialized, unsigned double characters array of words uninitialized array

efining W"R and S W"R ata Storage de'initions 'or signed and unsigned 3>9bit integers
val1 val2 val3 val4 DWORD SDWORD DWORD SDWORD 12345678h 2147483648 20 DUP(?) 3,2,1,0,1 ; ; ; ; unsigned signed unsigned array signed array

Assemb y !anguage 'or Inte 9,ased Computers

Chapter 3

Wendy Zhang, Computer Science & IT Department

efining .W"R / %+,%)/ Real ata Storage de'initions 'or 0uad(ords, tenbyte &a ues, and rea 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)

!ittle )ndian "rder o A data types arger than a byte store their indi&idua bytes in re&erse order o The east signi'icant byte occurs at the 'irst N o(estO memory address o /*amp e:
#a/) "4&%" )23-.567h

&dding 0ariables to &ddSub


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 10000h val2 DWORD 40000h val3 DWORD 20000h finalVal DWORD ? .code main PROC mov eax,val1 add eax,val2 sub eax,val3 mov finalVal,eax call DumpRegs exit main ENDP END main

; ; ; ; ;

start with 10000h add 40000h subtract 20000h store the result (30000h) display the registers

eclaring -nitiali1ed ata o +se the #dataK directi&e to dec are an unintia i-ed data segment:
.data?

o Within the segment, dec are &ariab es (ith EKE initia i-ers:
smallArray DWORD 10 DUP(?)

o Ad&antage: the programDs /V/ 'i e si-e is reduced

Assemb y !anguage 'or Inte 9,ased Computers

Chapter 3

Wendy Zhang, Computer Science & IT Department

.. Symbolic !onstants )Symbolic #efinition/ Associate and identi'ier Na symbo O (ith an integer e*pression or some te*t Symbo s do not reserve storage +sed on y by the assemb er (hen scanning a program 2annot change at run time )2ual'Sign irective o name F expression expression is a 3>9bit integer Ne*pression or constantO may be rede'ined name is ca ed a symbo ic constant o good programming sty e to use symbo s
COUNT = 500 . . mov al,COUNT

Calculating the Si1e of a +yte &rray o Current ocation counter: L o Subtract address o' ist o Di''erence is the number o' bytes o /*amp e:
list BYTE 10,20,30,40 ListSize = ($ - list) .ote: ListSize must 'o o( immediate y a'ter List

Calculating the Si1e of a Word &rray Di&ide tota number o' bytes by > Nthe si-e o' a (ordO
list WORD 1000h,2000h,3000h,4000h ListSize = ($ - list) / 2

Calculating the Si1e of a ouble3ord &rray Di&ide tota number o' bytes by = Nthe si-e o' a doub e(ordO
list DWORD 1,2,3,4 ListSize = ($ - list) / 4

).- irective o De'ine a symbo as either an integer or te*t e*pression# o Cannot be rede'ined
PI EQU <3.1416> pressKey EQU <"Press any key to continue...",0> .data prompt BYTE pressKey

o o o

%)4%).- irective De'ine a symbo as either an integer or te*t e*pression# Ca ed a text macro Can be rede'ined
continueMsg TEXTEQU <"Do you wish to continue (Y/N)?"> rowSize = 5 .data prompt1 BYTE continueMsg

Assemb y !anguage 'or Inte 9,ased Computers

?;

Chapter 3

Wendy Zhang, Computer Science & IT Department

count TEXTEQU %(rowSize * 2) setupAL TEXTEQU <mov al,count> .code setupAL

; evaluates the expression

; generates: "mov al,10"

0.

%eal1Address 'ode $rogramming 5enerate 67'bit MS' "S #rograms &dvantages o enab es ca ing o' BS9D$S and ,I$S 'unctions o no memory access restrictions isadvantages o must be a(are o' both segments and o''sets o cannot ca Win3> 'unctions NWindo(s WA on(ardO o imited to <=;) program memory Re2uirements o I.C!+D/ Ir&ine?<#inc o Initia i-e DS to the data segment:
mov ax,@data mov ds,ax

Add and Subtract, ?<9,it Persion


TITLE Add and Subtract, Version 2 INCLUDE Irvine16.inc .data val1 DWORD 10000h val2 DWORD 40000h val3 DWORD 20000h finalVal DWORD ? .code main PROC mov ax,@data ; mov ds,ax mov eax,val1 ; add eax,val2 ; sub eax,val3 ; mov finalVal,eax ; call DumpRegs ; exit main ENDP END main (AddSub2r.asm)

initialize DS get first value add second value subtract third value store the result display registers

Assemb y !anguage 'or Inte 9,ased Computers

??

Chapter 3

You might also like