Download Assembler & Data Rep in COSC 2410, Spring 2004, Univ. of Houston and more Quizzes Computer Architecture and Organization in PDF only on Docsity!
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
Lecture 2: Assembler
Overview
Dragan Mirkovic
Department of Computer Science
University of Houston
Introduction
• Announcements:
– The lab will start on 2/03/04.
– Quiz #1 (Number representations) 2/03/
– The course web page is available at
http://www.cs.uh.edu/~mirkovic/cosc2410/index.html
– Questions?
• Today’s lecture:
– A short overview of the IBM PC assembler
– Chapter 2 in Jones
– It is important that you start writing programs
– You can use the lab or any other resources
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
Representation Summary
- With n bits there are 2 n^ possible numbers that we can encode.
- How to encode positive and negative numbers together?
- Issues: ease of operations, uniqueness, simplicity
- Which one is the best? Why?
Unsigned Sign-Magnitude 1’s Complement 2’s Complement
More on Data Representations
- So far we have only discussed how to encode numbers
- What about letters, punctuation, symbols, etc.?
- The ASCII (American Standard Code for Information
Interchange) Character Set
- Encoding scheme used by all personal computers
- 7-bit ASCII encodes 128 characters
- 10 digits, (30H-39H)
- 52 upper- (41H-5AH) and lower- case (61H-7AH) letters
- 32 Control (non-printable) characters (0-1FH)
- 34 Punctuation and other symbols
- 8-bit IBM Extended character set contains encoding for 256
characters
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
Creating an Assembly Language Program
• You will get the details in the lab
• File names: 8-char name+extension
• Extension defines the file type
• File types:
.asm assembly source
.obj object file (compiler output)
.exe executable (linker output)
• Details in the Lab
Code development process
• Code development
process
– Use editor to create your
source (.asm)
– Compile your source
using MASM
– Link one or more object
files using link
– Execute your code
Editor (edit, pwb, …)
Source (.asm)
Compiler (MASM, …)
Linker (link, tlink …)
Object (.obj)
Executable (.exe)
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
Basic Elements of Assembly Language
- Identifiers:
- A programmer-chosen name. It can identify a variable, a constant,
a procedure or a code label.
- Constants:
- Integer, real, character, string, …
- Variables:
- Integer, real, arrays, strings, …
- Expressions:
- Operators (+,-,*,/…) and operands (variables, constants, …)
- Instructions and Directives
- Reserved words:
- Have special meaning and can only be used in their correct context
- Comments, …
Segmented Architecture
• Originally designed to enable 16-bit
processors to address more than 64K
• Each address is represented with a segment
and an offset
• In protected mode segments can separate
different blocks of code to protect them from
undesirable interactions
• 32-bit processors (starting with 80386) allow
a flat 32-bit offset addressing
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
Assembly program structure…
Assembly program structure…
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
Assembly program structure…
• Labels : optional, symbolic names (strings up
to 31 chars consisting of letters, digits and
special chars ‘@’, ‘$’, ‘_’ and ‘?’.)
• Operations :
– Executable machine instructions (mov, int, …)
– Non-executable (Directives, ex. .STACK, .DATA)
• Operands :
– Each operation can have 0 or more operands
Global Program Structure
• Preamble + three sections of the program
called SEGMENTS:
– Define subdivision of the memory used for special
purposes
– Introduced by the pseudo-operations (directives)
.STACK (required; used for subprograms)
.DATA (user data, variables etc.)
.CODE (executable instructions)
divided into procedures by the PROC/ENDP directive
pair.
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
Global Program Structure: Example
Preamble
• Starts with comments explaining the purpose,
giving the author, version of the program, etc.
– Not required but a good programming practice
– Specific form required for all your lab projects
(more about that in the lab)
• Contains a number of directives for the
compiler about
– The instruction set used
– Memory model used
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
.MODEL Directive
• Every module that uses simplified segments
must begin with the .MODEL directive
• Defines the attributes that affect the entire
module:
– Memory model
– Default naming and calling conventions
– Operating system
– Stack type
• Memory models: tiny, small, medium, …
• We will use small in most of the examples
- STACK: A last-in first-out data structure which is easy to
implement in hardware.
- The data is added or removed from the end of the stack
- In IBM PC Assembler stack is declared with the statement
.STACK n
- n ( should be even) is the number of bytes reserved for the stack.
- We shell use n = 100h (=256) in most of the examples
- Subprograms need to save and restore any registers they use
- The standard way to do that is to use the stack
Stack Segment
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
Multi-byte Storage Format
• The multi-byte data storage is architecture dependent
• There are two basic ways of multi-byte data storage
- Big-endian: The most significant byte is stored at the lowest address (big-end first) - Motorola, RISC
- Little-endian:The least significant byte is stored at the lowest address (little-end first). - Intel 80x86 microprocessors
• Other ways (middle-endian, rarely used )
• Intel 80x86 integer storage format:
Low byte High byte
Word
More on DATA Allocation
• The symbolic name is optional
• A sequence of values can be specified:
MyBytes DB 12, 22, 34
or
MyBytes DB 0ch ;
DB 16h ; [MyBytes+1]
DB 22h ; [MyBytes+2]
• The variable can be left uninitialized using?
Empty DD ?; Allocate uninitialized dword
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
String Allocation
• Bytes containing ASCII representation of characters
are defined using single (‘) or double (“) quotes:
Hi DB ‘H’, ‘e’, ‘l’, ‘l’, ‘o’
Hi DB ‘Hello’
Hi DB “Hello”
• Produces:
‘H’ ‘e’ ‘l’ ‘l’‘o’ = 72 101 108108111
• Repetition using the DUP operator:
Ones DB 100 DUP (1) ; 100 bytes 1,1,1,1,…
DB 10 DUP (2,3,?); 30 bytes 2,3,?,2,3,?,…
Data Representation and context
• Digital information = bits + context
– The same sequence of bits may have
different meaning depending on the
context
– Example:
pushad
popad
66606661 1717593697 f ` f a
Hexadecimal Integer String Command
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
Code Segment
• Starts with the .CODE directive
• One can use .STARTUP and .EXIT directives or not
• Example:
.CODE
.STARTUP
.EXIT
END
.CODE
start:
END start
• Startup will initialize some of the segment registers
(DS, SS, depending on the model used)
Code Segment…
• Otherwise we have to initialize segment registers
needed by the program and include exit to DOS call
.DATA
Message DB 'Hello, my name is Dragan Mirkovic', 13, 10, '$' .CODE Hello PROC mov ax, @data mov ds, ax …. mov al, 0 mov ah, 4ch int 21h Hello ENDP END Hello ;
Initialize DS register
Return to DOS Call
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
Program example
;; FIRST.ASM Our first Assembly Language Program. ;; .MODEL SMALL .586 ; Allows Pentium Instructions. Must come after .MODEL .STACK 100h .DATA Message DB 'Hello, my name is Dragan Mirkovic', 13, 10, '$' .CODE Hello PROC mov ax, @data mov ds, ax mov dx, OFFSET Message mov ah, 9h int 21h mov al, 0 mov ah, 4ch int 21h Hello ENDP END Hello ;
DOS Display String Call
• Instruction format:
mov destination, source ;
• It is really a copy operation
• The destination can be memory or register
• The source can be memory or register or constant
(but mem to mem is illegal!)
mov mem/reg, mem/reg/constant ;
• It can be used with DB, DW, DD operands, which
must have the same size
• The segment register cannot be initialized with a
constant
mov segreg, mem/reg ;
The mov Instruction
D. Mirkovic, COSC 2410: Computer Organization and Programming, Spring 2004
DOS Calls Examples
• The DOS Display string call
– Function code 9h
– Displays a string of ASCII characters whose
address is in the DX register, up to but not
including the first $ sign character.
.DATA
Message DB 'Hello, my name is Dragan Mirkovic', 13, 10, '$'
.CODE
Hello PROC mov ax, @data mov ds, ax mov dx, OFFSET Message mov ah, 9h int 21h
DOS Calls Examples…
• The exit to DOS call:
– In assembly an explicit return to the operating
system is required!
– Function code: 4ch
– Return code in AL
mov al, 0 ; Return code
mov ah, 4ch; Exit to DOS function code
int 21h
• Single instruction mov:
mov ax, 4c00h
Homework
• Exercises 2.2, problems 4,6a, 6b, and
7. pp. 44 in Jones.
• Read Chapter 2 in Jones.