Computer Architecture and System Software

Computer Architecture and
System Software
Lecture 06: Introduction to Assembly
Language Programming
Instructor:
Rob Bergen
Applied Computer Science
University of Winnipeg
Announcements

Assignment 2 posted
 Extension




depending on how far we get today
Assembly examples uploaded
Midterm marks posted
Feedback due today
Dosbox instructions.pdf was old file – ignore
 Use
instructions in todays lecture
Midterm

Observations:
 Confusion
between ‘exp’ and ‘E’ in FP numbers
 Confusion with interpreting signed numbers in Int and FP
reps
 Not many people knew how to perform long division
DOSBOX instructions (updated)




All files in assembly resources link (course website)
should be within directory you are working in
You need to enable the ‘show file extensions’ option
in your folder so you can modify a .txt to .asm (how
this is done varies with OS)
Create your text file within your working directory
and rename it from NameOfFile.txt to
NameOfFile.asm
Copy/paste the template into your file for a quick
start
DOSBOX instructions (updated)



Once you have a program ready to compile, run
DOSBOX
Mount your working folder by typing in
mount c “Directory” where directory is the location
of your working folder.
Example: mount c “C:\Windows\Assembly”
DOSBOX instructions (updated)




Change to your mounted folder by typing in ‘C:’
You may double check you’re in the right folder by
typing in ‘dir’ to display the directory. You should
see your file names now.
If you have a file name that is over 6 characters,
DOSBOX will shorten it in some manner like this:
LongFileName.asm  LONGFI~1.asm
If this has happened, you must type in the name as
it appears in your directory for all of the following
commands.
DOSBOX instructions (updated)



To compile your program:
masm NameOfFile.asm
or masm NAMEOF~1.asm
(depending on how long your filename is)
To link your file:
link NameOfFile
or link NAMEOF~1
(notice we DO NOT include ‘.asm’ at the end)
To run your file:
NameOfFile.exe
or NAMEOF~1.exe
DOSBOX instructions (updated)


Following all of the preceding steps in order will
allow you to compile, link, and run your program
Some shortcuts:
 Hitting
tab makes DOS search for a file/folder that
begins with what you’ve typed so far. This is handy for
quickly typing out long file names.
 Hitting up at the command line recalls your previous
commands.
Review of assembly so far

General registers AX,BX,CX,DX
 Subdivided
into 2 subregisters each: h (high) and l (low)
 Example: If ah = 04h and al = BFh, ax = 04BFh
 Registers contain numbers/addresses in hex form

Instructions:
 mov
destination, source
 Copies
value from source and overwrites destination. Source
remains unchanged
 add
destination, source
 destination
= destination + source. Source remains
unchanged. Destination overwritten with sum.
Assembly Example
data segment
numA dw 96 ;Define first variable
numB dw 21 ;Define second variable
data ends
…………………..
What are the contents of ax, bx,
mov ax, numA
at the end of this example?
mov bx, numB
add ah, bl
mov ah, 4ch
;Move interrupt code to ah subregister
int 21h
;Interrupt signal to return control to MS-DOS
………………………..
Interrupt Instructions





INT operand
Examples: See I/O website included with assembly
resources last lecture
Int 21h calls MS-DOS to perform a subroutine
Int 21h requires a subroutine code placed in register ah
The code placed in ah will determine which subroutine is
called
Interrupt
Subroutine 02h: Displays a character contained in
register dl
Example:
mov ah, 02h ;Store interrupt code in ah for char display
mov dl, bh ; Move some character from bh to dl for display
int 21h ; Interrupt signal for subroutine
Demo.asm, AdditionExample.asm



Demo.asm on the course website shows an example
of adding two numbers.
We want to display the results –
AdditionExample.asm shows the modifications
necessary to do this
Extra steps were necessary because we wanted to
display the character ‘6’. We need to use the ASCII
code for that character (6+30h).
How to interpret output



Addition example only works for one-digit sums
(can only display sums that equal 0-9)
We have to read/display digits of a number one at
a time
Example:
 Want
to read the number ’48’ typed by user and use it
in calculations
 Each digit interpreted as character
 Decode separately as 4 and 8
 Put the number 48 back together again

We’ll talk more about this in the future
Strings in Assembly




Recall direct memory addressing
All data is stored in data segment at an offset from
the starting address
Offset called effective address
Perhaps we have allocated 5 bytes and we want to
initialize the second byte only. How do we address
it?
Strings in Assembly
The following example initializes the first and second
bytes of a 5 byte array called table1, to the number
7 and 3:
table1 db 5 dup (?) ;definition of table1
mov [table1], 7
;move value into first address of table1
mov [table1+1], 7
;move value into first address of table1
Notice the square brackets indicate we are accessing
a memory location.
Accessing the second byte requires an offset of one.
[table1 + 1]
Strings in Assembly



Remember a string is just multiple characters stored
sequentially in memory.
It is very important to be able to access memory
directly to be able to work with strings
Now let’s look at how a string is printed in assembly
Strings in Assembly





Let’s look at the helloworld.asm example
Notice string is terminated by the characters ‘10’
and ‘$’
The $ is a requirement of the subroutine we’re
calling – strings must be terminated by this
character
Line-feed (ASCII 10D) is the control character that
brings the cursor down to the next line in the
terminal (like \n in C)
Some machines require a Carriage-Return (ASCII
13D) before the line-feed
Data transfer instructions
lea register, memory_address
 Examples
lea DX, mystring

Result: DX contains the effective memory address of
string ‘mystring’
Hello World – Subroutine 9h
After loading the memory address of the string we
move it to register dx
 Subroutine 9h starts at the memory address
contained in register dx – it will display a string of
characters until it finds the ‘$’ terminator
lea ax, myStr
mov dx, ax
mov ah, 9h
int 21h
Can you see how the above could be optimized?

Reading Characters




CharRead.asm shows a simple example of reading
a character using subroutine 1h
Subroutine reads one char into al, exits afterward
without output
Not very useful in most cases – we would prefer to
read an entire string at once.
This does present a good opportunity to learn how
to use one of the debug commands.
Debug and Tracing

After compiling your CharRead.exe file, you can
type ‘debug CharRead.exe’ in DOSBOX
Typing t (for trace), you can trace the contents of
registers one line at time.
Note that trace traces though interrupt calls as well,
so you will see more instructions that are apparent
in your source code.
‘t num’ traces through num lines at a time (ie. t 3)

When you’re done debugging, type q to quit



Buffered Keyboard Input



Called with subroutine code 0Ah
Reads an input string of characters that ends when
the user presses enter
Before we can call the subroutine, we’ll need space
to store the input string
Strings for buffered keyboard input

Normal string definition
myStr db ‘Hello World!’
 This

reserves a byte for each character: ‘H’, ‘e’, ‘l’…etc.
Buffered string definition
myBStr db 5, 5 dup(?)
 We
specify the max number of characters we want to
read first (5 in this example)
 We reserve 5 bytes for 4 characters + enter key (also
called carriage return)
Buffered Keyboard Input

Structure of buffered keyboard input in memory:
max | count | Buffer
1 byte |1 byte| N bytes
where max = max number to characters read
count = number of characters returned
(not including enter)
Buffer = contents of string
Buffered Keyboard input

With this structure in mind, what do the following
registers point to if myBStr is a buffered string?
a) Lea dx, myBStr
b) Lea dx, myBStr
inc dx
c) Lea dx, myBStr
add dx, 2
Buffered Keyboard input

With this structure in mind, what do the following
registers point to if myBStr is a buffered string?
a) Lea dx, myBStr
dx = maximum # of chars to read
b) Lea dx, myBStr
inc dx
dx = total number of characters (not including enter)
c) Lea dx, myBStr
add dx, 2
dx = First char of input string
Buffered Keyboard Input


Question: What happens if I overwrite the register I
used to load the buffered keyboard input? Is the
user input lost?
Answer: No – it was stored in memory.
Don’t forget the line feed!


After a user enters a string, we need to make room
on the command line for any other input/output for
later. (See BufferedExample.asm)
Therefore, after reading buffered keyboard input,
you should proceed it with the following 3 lines:
mov ah, 2h
Display subroutine
mov dl, 10
 Line feed character
int 21h
Call routine to
display character
Assignment 2


You should be able to complete assignment 2, now
that we’ve finished with strings.
Next, we’ll look at flags, and branching and loop
statements
Flag Register




Overflow flag: Set if a signed overflow occurs
Direction flag: Used to indicate direction of processing for
string manipulations (0 = forward, 1 = backward)
Interrupt enable flag: Setting this enables interrupts
Trace (trap) flag: Used for debugging. When set processor
executes only 1 instruction, then interrupts to call debugger

Sign flag: Set to the MSB of the result of an operation

Zero flag: Set if the result is zero



Auxiliary carry flag: Set if there was a carry from or borrow
to bits 0-3 in the AL register
Parity flag: Set if the number of1’s in result is even
Carry flag: Set if there was a carry from or borrow to the
MSB during last calculation
Image source: http://www.electronics.dit.ie/staff/tscarff/8086_registers/8086_registers.html
Flags Register

Purpose: We can ask use the flags to ask questions
about the contents of registers
 Are
contents equal?
 Are contents the same sign?
 Is contents of one register larger than another?
 etc
Branching and Loop Instructions

JMP used to make the program execution jump to a
specific label or address
 Called

jmp label
 Label

an unconditional jump since jump always occurs
identifies the next instruction to be executed
Example
mov ax, 1
inc_again: inc ax
jmp
inc_again
mov
bx, ax
CMP instruction


cmp source, destination
Performs the same operation as sub (subtraction)
but does not update any registers. Only updates
flags.
Branching and Loop Instructions

J<cond> used to make the program execution jump to a specific
label or address if the condition is true

j<cond> label

Example
read_char:
mov dl, 0
…
;Code for reading a character into al
cmp al, 0Dh
;Compare the character to ODh
je CR_received ;if equal, jump to CR_received
inc cl
;otherwise, increment cl and
jmp read_char ;go back to read another
CR_received:
…
;character from keyboard
Branching and Loop Instructions

Note, while the result is not saved anywhere, the operation sets the
zero flag (ZF = 1) if the two operands are the same

je
jump if equal

jg
jump if greater

jl
jump if less

jge jump if greater than or equal

jle

jne jump if not equal

jz
jump if zero

jnz
jump if not zero

jc
jump if carry

jnc
jump if not carry
jump if less than or equal
Branching and Loop Instructions
How is iteration performed?
mov cl, 50
repeat1:
<loop body>
dec cl
jnz repeat1
;jumps back to
;repeat1 if cl is not 0

Logical Instructions








AND, OR, XOR, NOT
and destination, source
or destination, source
xor destination, source
not destination
The first three are binary operators and perform bitwise
and, or, and xor logical operations respectively.
The not is a unary operator that performs bitwise
complement operation
Example:


and al, 01h
Note: logical operations set the zero flag if the result is 0
Logical Instructions Example
and AL,01H
;Assume Al contains= 0110 1010
jz bit_is_zero
;<code to be executed when the bit is one>
jmp skip1
bit_is_zero:
;<code to be executed when the bit is zero>
skip1:
;<rest of the code>
Logical Instructions Example



In the last example the first instruction compared a
byte 0110 1010 to 0000 0001
The and operation sets al to 0000 0000 (no bits
match)
The example illustrated how a logical instruction
could be used as a conditional, but we may not
want to overwrite al
Logical Instructions


TEST performs logical bitwise and operation like the
and instruction except that the source and
destination are not modified
However, test sets the flags just like the and
instruction
Shift Instructions

SHL, SHR Shift logical left and shift logical right

shl dest, count
shr dest, count

shl dest, cl
shr dest, cl




The destination can be an 8, 16, or 32-bit operand stored either in
a register or in memory
The second operand specifies the number of bit positions to be
shifted
The first format specifies the shift count directly. The shift count can
range from 0 to 31
The second format can be used to indirectly specify the shift count,
which is assumed to be in the cl register. The cl register contents are
not changed by either the shl or shr instructions
Shift Instructions

Examples
Shift Instructions




SAL, SAR Shift arithmetic left and shift arithmetic right
ssl dest, count
sar dest, count
ssl dest, cl
sar dest, cl
Examples
Rotate Instructions



ROL, ROR Rotate left and right without carry
rol dest, count
ror dest, count
rol dest, cl
ror dest, cl
Rotate Instructions

Examples
Rotate Instructions



RCL, RCR Rotate left and right through carry
rcl dest, count
rcr dest, count
rcl dest, cl
rcr dest, cl
Rotate Instructions

Examples
Flag Instructions








CLC – Clear carry flag
STC – Set carry flag
CLD – Clear direction flag
STD – Set direction flag
CLI – Clear interrupt flag
STI – Set Interrupt flag
CMC - Complement Carry flag
LAHF – Load AT from 8 low bits of flags register
Stack Instructions

PUSH operand


POP operand




Operand could be reg, memory
PUSHA – push all general purpose registers onto the stack


Operand could be reg, memory, or immediate
AX, CX, DX, BX, SP, BP, SI, DI
POPA – get all general purpose registers from the stack
PUSHF – store flags register in the stack
POPF – get flags register from the stack
Quick Reference








dl – display register (use subprogram 2h)
al – read char register (use subprogram 1h)
ah – storage for subprogram codes
dx – register for storing strings to be displayed (use
subprogram 9h)
4ch – code for return to DOS
0Ah – code for buffered keyboard input
13 – carriage return ASCII code (note: base 10 #)
10 – line feed ASCII code (note: base 10 #)
Lab 07




FP multiplication review
Tracing register contents through assembly
programs
Know how to use the basic interrupt subroutines
(read/display)
Don’t need to know buffered keyboard input for this
lab