Implementing Standard Program Structures in 8086 Assembly Language - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Implementing Standard Program Structures in 8086 Assembly Language

Description:

Chapter 4. Implementing Standard Program Structures in 8086 Assembly Language. CS200 Chapter 4 ... RCL, RCR. rotates each bit 1 position to the left/right ... – PowerPoint PPT presentation

Number of Views:1264
Avg rating:3.0/5.0
Slides: 33
Provided by: anniegro
Category:

less

Transcript and Presenter's Notes

Title: Implementing Standard Program Structures in 8086 Assembly Language


1
Chapter 4
  • Implementing Standard Program Structures in 8086
    Assembly Language

2
Review of Data Types
  • Built-in types
  • DB, DW, DD
  • Declaring variables
  • var DB 27h declares a variable and inits to
    27h
  • var DW ? declares a variable and inits to ?
  • Using variables
  • mov ah, var move the contents of var to ah
  • mov ah, var same as above
  • mov ax, var illegaltrying to move 8 bit
    value to 16 bit register
  • mov ax, offset var legalmoves 16 bit address
    to 16 bit register

3
Review of Creating Logical Segments
  • Provide segment identifier, then wrap
    instructions/declarations with segment ends
  • DATA SEGMENT
  • var DW 3456h
  • DATA ENDS
  • CODE SEGMENT
  • NOP instructions go here
  • CODE ENDS
  • Use the predefined macros in MASM
  • .DATA
  • var DW 3456h
  • .CODE
  • NOP instructions go here

4
Using the Assume Directive
  • Not necessary when you are using the Masm
    predefined keywords .DATA, .CODE, .STACK
  • its an instruction to the assembler, not the
    processor
  • only tells the assembler to assume that the
    register is correctly initialized
  • it does not generate any machine code
  • you are still required to load the DS register if
    you are creating a data segment

5
Simple Example
  • .model small
  • .dosseg
  • .data
  • greeting db 'hello there',13,10,''
  • .code
  • mov ax,_at_data
  • mov ds,ax
  • mov ah,9
  • mov dx,offset greeting
  • int 21h
  • mov ah,4ch
  • int 21h
  • end

6
How to write an assembly language program
  • define the problem
  • write the algorithm
  • translate
  • what type of data am I working with?
  • initialization instructions
  • instructions to implement algorithm

7
Problem Average 2 numbers
  • Algorithm
  • add maximum temp and minimum temp
  • divide sum by 2 to get average
  • Translate
  • whats our data?
  • what are our initialization steps?

8
  • .model small
  • .dosseg
  • .data
  • max_temp DB 92h max_temp storage
  • min_temp DB 52h min_temp storage
  • avg_temp DB ? avg_temp storage
  • .code
  • .startup used
    .startup instead of start
  • mov ax,_at_data initialize data
    segment
  • mov ds,ax
  • mov al, max_temp get first temp
  • add al, min_temp add second temp
    to first temp
  • mov ah, 00h clear ah
    register
  • adc ah, 00h put carry
    in lsb of ah
  • mov bl, 02h load
    divisor into bl register
  • div bl divide
    ax by bl..quotient in al, and remainder in ah
  • mov avg_termp, al copy result to
    avg_temp
  • .exit
    .exit generates the instructions to return
    control to msdos
  • end no
    need to specify a label because we used .startup

9
Tips for Debugging
  • Be sure to work out an algorithm firstnot as you
    go along
  • write and test sections of a larger program,
    rather than waiting until you think youre done
    to do any testing
  • make sure you coded according to a correct
    algorithm
  • add a set of eyes
  • use a debugger
  • single step through code
  • strategically place breakpoints

10
BCD Packing Program
  • When typing a digit from the keyboard, it is
    represented in ASCII values 30H thru 39H
  • 00110000 thru 00111001
  • if we replace the 0011 in the upper nibble, we
    are left with BCD code for the digit
  • this is an unpacked BCD number because we are
    using 8 bits to represent it
  • We can combine 2 BCD digits in a single bytethis
    is a packed BCD number

11
Algorithm
  • Convert the first ascii number to unpacked BCD
  • Convert the second ascii number to unpacked BCD
  • Move the first nibble to upper nibble position in
    byte
  • Pack 2 BCD nibbles in one bye

12
Masking
  • Masking a bit
  • if we AND a bit with 0, the result is always 0
  • we have hidden(masked) the previous state of the
    bit
  • Masking a nibble
  • the smallest data we can AND is a byte
  • if we AND a bit with a 1, we have no effect on
    the bit
  • to mask the high nibble
  • AND AL, 0FH AND AL with 00001111 to mask upper
    nibble

13
Rotate
  • This is how we can move the lower nibble to the
    upper nibble
  • MOV CL, 04H store value in register
  • ROL BL, CL rotate left 4 bits
  • Rotate instructions
  • ROL, ROR
  • rotates each bit 1 position to the left/right
  • MSB is moved into LSB, and into CF
  • RCL, RCR
  • rotates each bit 1 position to the left/right
  • MSB is moved into CF, CF is moved into LSB

14
Combining Bytes
  • We cant use a MOV, because we will erase the
    contents of one of the bytes
  • adding would work though
  • ADD AL, BL
  • can also use an OR
  • OR AL, BL
  • this works because ORing a bit with 0 leaves the
    bit the same

15
The Assembly Code
  • .code
  • .startup used
    .startup instead of start
  • .exit
    .exit generates the instructions to return
    control to msdos
  • end no
    need to specify a label because we used .startup

16
Jumps
  • Conditional
  • need to look at the ST register to evaluate the
    state of particular flags
  • the instruction specifies which flags to look at
  • this determines whether to fetch the next
    instruction from the jump destination or from the
    next sequential memory location
  • Unconditional
  • always jumps to the specified jump destination

17
JMP - unconditional jump
  • Near vs. far
  • near if the jump destination is in the same code
    segmentintrasegment
  • far if the jump destination is in a different
    code segmentintersegment
  • direct vs. indirect
  • direct if the jump destination address is
    specified directly in the instruction
  • indirect if the jump destination address is
    contained in a register or memory

18
Types of Unconditional Jumps
  • For near-type jumps, the destination must be in
    the same segmentany location from 32,767 to
    -32,768 bytes from the IP
  • if the displacement is positive, its a forward
    jump, otherwise its a backward jump
  • short-type jump instructions
  • any location from 127 to -128 bytes from the IP
  • well be primarily using direct near-type jumps

19
Jump Examples
20
Review of Conditional Flags
  • Carry flag
  • addition
  • if the sum is greater than 16 bits,carry flag is
    set to 1
  • subtraction
  • if the bottom number is larger than the top
    number, this is a borrow flag, and is set to 1
  • comparison, CMP BX, CX
  • if BX gt CX, CF 0, ZF 0
  • if BX lt CX, CF 1, ZF 0
  • if BX CX, CF 0, ZF 1

21
  • Parity flag
  • if the lower 8 bits of the destination operand
    has an even number of 1s, parity flag 1
  • Auxiliary Carry flag
  • significant only when doing BCD addition or
    subtraction
  • represents a carry out when the least significant
    nibbles of 2 bytes are added or a borrow when
    subtracting the least significant nibbles
  • used by the instructions DAA(Decimal Adjust after
    Addition), and DAS(Decimal Adjust after
    Subtraction)
  • Zero flag
  • set to 1 if the result of an arithmetic operation
    is 0
  • used by SUB, AND, CMP, INC, DEC

22
  • Sign flag
  • 8086 uses 2s complement, sign-and-magnitude form
    for representing negative numbers
  • MSB represents sign bit
  • lower 15 bits represent the magnitude
  • set to 1 if the number is negative
  • can be used to indicate if a number has been
    decremented beyond zero
  • Overflow flag
  • set to 1 if the result of a signed operation is
    too large to fit in the number of bits available
    to represent it
  • indicates that the result has overflowed into the
    sign bit

23
Conditional Jump Instructions
  • The conditional jump instruction looks at the
    state of the flag register to determine whether
    to jump or not
  • must be a short type jumpthe destination must be
    in the range of 128 or -127 bytes from the
    address of the instruction after the jump
    instruction
  • the terms above and below are used when dealing
    with unsigned numbersgreater and less are used
    with signed numbers

24
Conditional Jump Examples
  • Implementation A
  • CMP AX, BX compare ax to
    bx to set flags
  • JE THERE if
    equal, then skip correction stmt
  • ADD AX, 0002H correction
    stmt
  • THERE MOV CL, 07H load count
  • Implementation B
  • CMP AX, BX compare
    ax to bx to set flags
  • JNE FIX if
    not equal, then correct
  • JMP THERE
    unconditional jump to there
  • FIX ADD AX, 0002H correction
    stmt
  • THERE MOV CL, 07H load count

25
  • Implementation A
  • works well for a short sequence of instructions
    after the conditional jump
  • what if the jump destination is greater than 127
    bytes from instruction?
  • Implementation B
  • will always work
  • requires an unconditional jump which can jump to
    anywhere
  • We can use these to implement selection
  • IF-THEN and IF-THEN-ELSE
  • rememberall conditional jumps are short-type
    jumps!

26
IN and OUT Instructions
  • These instructions allow us to read from or
    output to I/O ports
  • IN
  • can input from a fixed portmust be an 8-bit
    address
  • IN AL, 04H Read in a byte of data from port
    04h
  • IN AX, 04H Read in 2 bytes of data from
    port 04h
  • or a variable port port address must be
    contained in the DX register
  • MOV DX, 0FFF8H Load port address 0fff8h to
    DX
  • IN AL, DX Read in a byte of data from port
  • IN AX, DX Read in 2 bytes of data from port

27
  • OUT
  • can output to a fixed portmust be an 8-bit
    address
  • OUT 0AH, AL output a byte of data to port
    0ah
  • OUT 0AH, AX output 2 bytes of data to port
    0ah
  • or a variable port port address must be
    contained in the DX register
  • MOV DX, 0FFFAH Load port address 0fffah to
    DX
  • OUT DX, AL output a byte of data to port
  • OUT DX, AX output 2 bytes of data to port
  • Why use one versus the other?
  • Can specify more ports using a 16 bit address
  • variable ports allow the port address to change
    during execution

28
Printed Circuit Board Making Machine
29
More on Conditional Jumps
  • What other control flow structures can we use
    them for?
  • While-Do
  • Repeat-Until
  • How?
  • perform comparison
  • place jump label in appropriate position

30
Arrays in Assembly Programs
  • Declaring them
  • myarray DW 10 DUP (1) declares an array of 10
    elements
  • yoarray DB 25 DUP (?) declares an array of
    25 elements
  • grades DB 98, 87, 34, 65,
  • 90, 88, 92, 91 declares
    an array of 8 elements
  • Using them
  • MOV AL, grades0 copies 98 to AL
  • MOV BX, 0000h initialize BX to 0
  • MOV gradesBX, 97 sets gradesBX to97

31
  • Indexing into Arrays
  • array indexing is the same as in high-level
    languages for byte arrays
  • slightly more complicated for word or double
    arrays
  • general purpose formula
  • nth element of array array(n -1) size of
    element
  • where size of element 1 for byte, 2 for word, 4
    for double
  • Loading the address of Arrays
  • LEA BX, grades Load the address of grades into
    BX
  • Could have used
    MOV BX, offset grades
  • MOV AL, BX Copy first element in grades to
    AL
  • INC BX Increment BX
  • ADD AL, BX Add to AL the next element in
    grades
  • INC BX Increment BX
  • MOV BX, AL Store sum in next slot of
    grades array

32
The LOOP Instruction
  • Implements the FOR-DO control structure
  • a for loop in some high level languages
  • uses the CX register as a counter, and does
    auto-decrementing
  • first decrements CX by 1, then checks if CX 0
  • XOR AX
    Init AX to 0
  • MOV CX, 0004H
    Initialize the counter
  • LOOP_EXAMPLE ADD AX, CX Sum
  • LOOP
    LOOP_EXAMPLE
Write a Comment
User Comments (0)
About PowerShow.com