Title: Things I learned from grading HW2
1Things I learned from grading HW2
- a leading 0 tells a86 to interpret the number as
hexMov al, 27 al ? 2710Mov al, 027 al ?
2716
Things to be careful of
- A program is not debugged by checking a single
case - Dont assume that registers are zero unless you
set them to zero
2About comments
- This is NOT a comment, it is redundantCmp AH,
0 AH - 0, result not storedJE L1 jump to
L1 if AH 0 - A comment conveys extra information Cmp AH, 0
If the remainder is zero, then itsJE L1 an
integer multiple of 39--so quit
3Homework for Thursday, September 2
- Write, assemble, and debug an 8088 assembly
language program which includes the following
statements - Numbytes db 9
- Array db 1,2,2,3,3,3,4,4,5
- Target db 2
- Count db ?
- The program should use a loop with indexed
addressing to count the number of occurrences of
target in Array. - Debug your program by changing the value of
Target in the debugger and making sure the
program works. Be sure to try a target which is
not included in Array.
4Flow Chart
5Program to solve HW
Jmp gt L1 Numbytes db 9 Array db
1,2,2,3,3,3,4,4,5 Target db 2 Count db
? L1 Mov Ch,0 Mov CL, Numbytes CX set to go
through Array Mov Count, 0 count number of
hits Mov Si, 0 for indexed addressing Mov
Bl, target to avoid memory/memory
instructions L2 Cmp Array Si,Bl jne gt
l3 if not equal, dont increment inc
count l3 inc si point to next byte Loop
L2 l4 a destination for g command
68088 Addressing
- 20 Address/Data lines access 8088 memory
- CS, DS, ES, SS are the four 16-bit internal
segment registers - These are combined with another 16-bit register
to generate the 20-bit address - CSIP are combined to point to the next
instruction/data in memory.Effective address
CS 16 IP CS 1016 IP - CS A100, IP 1234, Effective address is A1000
1234 A2234
7.com vs .exe
- A com file is created when CS DS ES SS
- All parts of the program fit within 64K
- (64K is the address range of a single 16-bit
register, like IP) - An exe file is created when two different
memory segments are used.
8The stack
- The current stack location is defined by SSSP
- The stack is used for some operations like CALL
and RET - Programmers may use the stack as scratchpad
memory - PUSH puts a word onto the stack
- POP pulls a word from the stack
- The stack is a LIFO (Last-in, First-Out) register
9Stack Pointer
- The stack pointer, SP, always points to the most
recently filled location in the stack - The SP is initialized at the bottom of the
segmentAs words are added to the stack, the
stack fills up towards the beginning of the
segment. - Thus, after a PUSH instruction, SP ? SP -2after
a POP instruction, SP ? SP 2 - 8088 assembly language allows only one word-wide
operand to Push and PopA86 allows multiple
operands and converts them into multiple
instructions at the time of assembly
108088 Procedures (Subroutines)
- Call dest
- Address of next instruction is pushed onto the
stack - Therefore, SP ? SP-2
- IP ? dest
- RET (no operand)
- IP ? return address from the stack
- Therefore, SP ? SP 2
11Three ways to crash your computer
- The RET instruction takes the address of the next
opcode from the stack - If this address is wrong, the results are
unpredictable - Within a procedure, Push and Pop instructions
must occur in pairs - CRASH, if inside a procedure
- the number of Pushes exceeds the number of Pops,
or - the number of Pops exceeds the number of Pushes,
or - the program changes the SP and does not restore
it to its original value before RET
12Push and Pop and Procedures
- Push and Pop are used to preserve registers when
a procedure is called
Push Ax, BxCall DOITPop Bx, AX DOit
manipulations involving AX and BXRET
Call ADDIT Addit Push Ax, Bx
manipulations involving AX and BX
Pop Bx, AX RET
vs
Which is better programming?
13Good Programming
- A procedure should return with all registers
unchanged, except for registers used for output!
Call ADDIT Addit Push Ax, Bx
manipulations involving AX and BX
Pop Bx, AX RET
As a practical matter, otherwise the programmer
must remember to Push and Pop every time the
procedure is called.
14CallRet.485
Use this program and debugger to understand Call,
Ret, Push, and Pop
mov al, 3 mov bl, 5 call mulab mulab sets cx
albl and doesn't change al or bl mov
dx,cx mulab push ax,bx to save registers mul
bl mov cx,ax pop bx,ax to restore registers ret
15a86 switches and DOS
- Path c\ c\dos c\a86 c\a86\a86lib c\util
- The path statement tells dos what directories to
look in when its searching for an executable
file(.com, .exe, .bat) - set a86P64
- this tells a86 to create machine language
instructions for the 8088 instruction
setOtherwise a86 will assemble for the
computers processor - set a86libc\a86\a86lib
- When a86 encounters a symbol that it does not
know, it looks in the library. This switch tells
a86 where to find the library
16a86 libraries
- Step 1) Create a useful procedure.
- Make sure labels and variable names are unique
- Procedures should primarily use local labels
- Step 2) a86lib filename.ext
- a86lib.com is a registered-user-only executable
file that adds the procedure to the library
indexer, a86.lib - Step 3) at assembly, when a86 encounters a
procedure name that it does not know, it looks to
a86.lib (the set a86lib switch tells it where to
look). - If the procedure is found there, the ASCII text
of the procedure is appended to the program being
assembled - Step 4) a86 assembles the program the appended
procedures into a machine-language executable - This is different than a mainframe executable
file library.
17licensing considerations
- a86 and d86 are Shareware. Anyone may download
and test a86 and d86 to determine if it will meet
his or her needs. Someone who decides to use it
is obligated to pay the fee and license the
product. - The University of Mississippi has purchased a
site license for a86/d86. a86, d86, and
a86lib.com can be used on any university-owned
computer. - a86lib.com is restricted to licensed users only.
Possession or use of a86lib.com outside the
university is a violation of the law, unless the
user has paid the license fee. - Libraries created with a86lib.com (the a86.lib
file) are not licensed and may be distributed.
Executables may be distributed.
18EE 485/486 and libraries
- Unless specifically prohibited, you can assume
the standard library procedures are present when
writing any homework assignment or answering any
test question in EE 485 or EE 486