Nothing Special   »   [go: up one dir, main page]

Learning Assembly Language - Part 01

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Computer Organization and

Assembly Language (CS235)


Fall
2013

Introduction to Assembly Language


Muhammad Jameel <muhammad.jameel@seecs.edu.pk>
Basic Elements of Assembly Language

• Basic Elements of Assembly Language

• Example: Adding and Subtracting Integers

• Assembling, Linking, and Running Programs

• Defining Data

2
Basic Elements of Assembly Language

• Integer constants & expressions


• Character and string constants
• Reserved words and identifiers
• Directives and instructions
• Labels
• Mnemonics and Operands
• Comments

3
Basic Elements of Assembly Language

• Reserved words
– Reserved words cannot be used as identifiers
– Instruction mnemonics, directives, type attributes,
operators, predefined symbols
– Examples: BYTE, MOV, ADD
• Identifiers
– To identify a variable, a constant, a label, a proc.
– Examples: $first, _main, @myfile

4
Basic Elements of Assembly Language

• Directives
– Used to declare code, data areas, select memory
model, declare procedures, etc.
– not case sensitive
– Example: .data, .code

• Different assemblers have different directives

5
Basic Elements of Assembly Language

• Instructions
– Assembled into machine code by assembler
– Executed at runtime by the CPU
– An instruction contains:
• Label (optional)
• Mnemonic (required)
• Operand (depends on the instruction)
• Comment (optional)

6
Basic Elements of Assembly Language

• Labels
– Follow identifier rules
– Data label
• must be unique
• example: myArray (not followed by colon)
– Code label
• target of jump and loop instructions
• example: L1: (followed by colon)

7
Basic Elements of Assembly Language

• Instruction Mnemonics
– Identify the operation carried out by an instruction
– examples: MOV, ADD, SUB, MUL, INC, DEC
• Operands
– constant
– constant expression
– register
– memory (data label)
– Example: MOV count, bx : INC ax
8
Basic Elements of Assembly Language

• Comments
– Single-line comments
• begin with semicolon (;)
• Example: ; my code goes here
– Multi-line comments
• begin with COMMENT directive and a programmer-
chosen character
• Example: COMMENT &
These lines are commented
&
9
An Assembly 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 procedures here)
END main

10
Example (Assembly program)
• ; 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

11
Example Program 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

12
Basic Elements of Assembly Language

• Basic Elements of Assembly Language

• Example: Adding and Subtracting Integers

• Assembling, Linking, and Running Programs

• Defining Data

13
Assembling, Linking, and Running Programs

• Assemble-Link Execute Cycle


Link
Library
Step 2: Step 3: Step 4:
Source assembler Object linker Executable OS loader
Output
File File File

Listing Map
Step 1: text editor File File

14
Make32.bat

• Called a batch file


• Contains a command that executes ML.EXE
• Contains a command that executes LINK32.EXE
• Command-Line syntax:
make32 progName
(progName does not include the .asm extension)

Use make16.bat to assemble and link Real-mode programs

15
Listing File

• Use it to see how your program is compiled


• Contains
– source code
– addresses
– object code (machine language)
– segment names
– symbols (variables, procedures, and constants)
• Example: addSub.lst

16
MAP File

• Information about each program segment:


– starting address
– ending address
– size
– segment type
• Example: addSub.map (16-bit version)

17
Data Types

• BYTE, SBYTE
– 8-bit unsigned integer; 8-bit signed integer
• WORD, SWORD
– 16-bit unsigned & signed integer
• DWORD, SDWORD
– 32-bit unsigned & signed integer
• QWORD
– 64-bit integer
• TBYTE
– 80-bit integer

18
Defining BYTE and SBYTE Data

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

19
Defining Byte Arrays

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

20
Defining Strings

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

21
Defining Strings

menu BYTE "Checking Account",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

22
Defining Strings

menu BYTE "Checking Account",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

End-of-line character sequence:


0Dh = carriage return
0Ah = line feed

23
Using the DUP Operator

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”

24
Defining WORD and SWORD Data

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

25
Defining QWORD, TBYTE, Real Data

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)

REAL4
4-byte IEEE short real
REAL8
8-byte IEEE long real
REAL10
10-byte IEEE extended real

26
Little Endian Order

All data types larger than a byte store their individual


bytes in reverse order. The least significant byte occurs at
the first (lowest) memory address.

Example:
val1 DWORD 12345678h

27
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 10000h
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

28

You might also like