(Missed the first hour of
class)
Some definitions:
Variable name
is the identifier associated with a location in memory. Value
may be stored or retrieved from the memory location. (Long discussion
below)
Identifiers
may be up to 256 characters long. This can be controlled by a
setting, which by default is 31 characters. The first character
must be alphabetic (a..z, A..Z) or _ $ @ ? and . (the period).
However these characters also have special significance, so make
the identifier start with a letter. Assembler won't assemble it.
See p. 18-19 in the book for further explanation and examples.
Label:
name of a line of code. Defined by where you write it.
For example, x: mov ax,
3 Then: jmp x would make the ip register point to the
line of code that said mov ax, 3. Could also say y: jmp
y (infinite loop), jmp x+3 is also legal. Jmp bx would cause it
to
Offset:
The distance from the beginning of a segment (generally,
the data segment) to the beginning of the variable (leftmost
byte).
When I write .data
name db "James Jones",
0Ah, 0Dh, "$"
x dw 13
y dw 14
The offset of x means the
distance from the beginning of the data segment, so the offset
of x is 14 (you must start counting at 0).
y has an offset of 16. (since
14 & 15 are both consumed by x, which is a word).
ds itself then points to "J".
What kinds of symbols can
we define?
db define byte
dw define word
dd define double word
df define far pointer
dq define quad word (8 bytes)
dt define 10 bytes
When defining symbols, you
can optionally have a name and initial value:
[name] db [init val [,init
val]]
db 'A' ASCII
db -128 signed decimal value
db +128 signed decimal value
db 0 unsigned
db 255 largest unsigned
list db 10,20,30 each takes
up 1 byte
char db 'a'
hex db 17h
name db "James Smith"
thing db $-name ; $ is the
value of the location counter,
; which counts the memory
used so far.
; So, thing is at the next
available location, but since
; $-name will give you the
length of name, its value is 11.
Reversal storage format:
Assembler reverses the bytes
when storing them in memory.
dq 01030507h would store
as 07050301 in memory
--------------------------------------------------------------------
Discussion resulting from
the definition of variable name.
(Long discussion of hex vs.
ASCII omitted.)
Given:
.data
x db 15
y db 3
employee db "Jones,
John"
nums db 1,3,5,7,9
sum db ?
the first "J" is
at data + 3, also known as employee + 0 or y+1.
MOV AL, x+5 ; this would put
an "n" into AL
some code to add the numbers
into nums using relative addressing:
mov al, nums ; al now contains
1
add al, nums+1 ; adds 3 to
al (now contains 4)
add al, nums+2 ; adds 5 to
al (now contains 9)
add al, nums+3 ; adds 7 to
al (now contains 16)
add al, nums+4 ; adds 9 to
al (now contains 25)
mov sum, al
Instead of using a number,
we could use a register to do the same thing.
.code
mov ax, @data ; get address
of data segment
mov ds, ax ; point ds to
the data segment
mov al, nums ; knows how
to find nums because of ds
mov bx, 0
mov al, nums+bx ; copies
nums+0 = 1 into al
mov al, nums[bx] ; same as
above (see c & Pascal code below)
inc bx ; adds 1 to bx
add al, nums[bx] ; now adds
nums+1 = 3 into al (now contains 4)
c code for mov al, nums+0
nums[0]
*(nums+0)
Pascal code for the same thing
nums[0]
(nums+0)^
inc nums would add 1 to the
memory location pointed to by nums, so that the first # in nums
would be 2 instead of 1.
Keep in mind that nums+2 points
to the location 2 bytes after the beginning of nums, while add
nums, 2 adjusts the value in the first byte pointed to by nums.
Even in nums was defined as a word (dw) instead of a byte, nums+2
would point to the location 2 bytes after the beginning of nums.
In c, given int * ip
ip++ will point to the location
2 bytes after ip, because an int takes up 2 bytes.
Pascal does the same thing:
given ip: ^integer,
ip+1 will point to the location
2 bytes after ip. BUT, in Pascal, if I said: ip: Pointer, ip+1
the compiler wouldn't allow it because it doesn't know what ip
is pointing to. This helps prevent "shoot self in foot"
numw dw 11, 9, 13, 15
mov ax, numw ; 11 into ax
add ax, numw+2 ; adds 9, result
is 20
add ax, numw+4 ; adds 13,
result is 33
add ax, numw+6 ; adds 15,
result is 48
numw is: 000B 0009 000D 0005
stored as: 0B00 0900 0D00
0500
if you said mov ax, nums+3
you would move 00 0D into
ax, but since it gets flipped when you move it, you move 0D00
into ax. Big time shot in foot.
--------------------------------------------------------------------
Every program must have the
following elements:
title ; put in the title here
.model small
.stack 100h
.data
.code
main proc
mov ax, @data
mov ds, ax
; your code goes here
mov ax, 4c00h
int 21h
main endp
end main
For example, if you make a
file called template.asm, you can load it up every time and type
into it, save it under a new name.
.data
ary db 1,13,2, 12,3,11,4
bry dw 17,16,15,16,4
bptr dw bry
.code
main proc
mov ax, @data
mov ds, ax
mov bx, bptr
mov ax,[bx]
push bry stack 17
push bry+2 16
push bry+4 15
push bry+6 16
push bry+8 4
pop ax 4
pop ax 16
pop ax 15
pop ax 16
pop ax 17
To do today
Page 73, exercises 1, 2 and
3.
Next week, hand in 8, 9 and
10. (Ignore the 0200-type stuff -- do these exercises in TASM)