Saturday 17 February 2018


8086 ASSEMBLY PROGRAM FOR ADDITION OF TWO 8 BIT NUMBERS
data segment
a db 09h
b db 02h
c dw ?
data ends

code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov al,a
mov bl,b
add al,bl
mov c,ax
int 3
code ends
end start

C:\TASM>masm an8add.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987.  All rights reserved.

Object filename [an8add.OBJ]:
Source listing  [NUL.LST]:
Cross-reference [NUL.CRF]:

  50402 + 450254 Bytes symbol space free

      0 Warning Errors
      0 Severe  Errors

C:\TASM>link an8add.obj

Microsoft (R) Overlay Linker  Version 3.60
Copyright (C) Microsoft Corp 1983-1987.  All rights reserved.

Run File [AN8ADD.EXE]:
List File [NUL.MAP]:
Libraries [.LIB]:
LINK : warning L4021: no stack segment

C:\TASM>debug an8add.exe
-g

AX=0B0B  BX=0002  CX=0022  DX=0000  SP=0000  BP=0000  SI=0000  DI=0000
DS=0B97  ES=0B87  SS=0B97  CS=0B98  IP=0011   NV UP EI PL NZ NA PO NC
0B98:0011 CC            INT     3
-d 0B97:0000
0B97:0000  09 02 0B 0B 00 00 00 00-00 00 00 00 00 00 00 00   ................
0B97:0010  B8 97 0B 8E D8 A0 00 00-8A 1E 01 00 02 C3 A3 02   ................
0B97:0020  00 CC 86 72 FF 77 15 8A-86 70 FF 2A E4 50 B8 FD   ...r.w...p.*.P..
0B97:0030  05 50 FF 36 24 21 E8 77-63 83 C4 06 FF 36 24 21   .P.6$!.wc....6$!
0B97:0040  B8 0A 00 50 E8 47 5E 83-C4 04 5E 8B E5 5D C3 90   ...P.G^...^..]..
0B97:0050  55 8B EC 81 EC 84 00 C4-5E 04 26 80 7F 0A 00 74   U.......^.&....t
0B97:0060  3E 8B 46 08 8B 56 0A 89-46 FC 89 56 FE C4 5E FC   >.F..V..F..V..^.
0B97:0070  26 8A 47 0C 2A E4 40 50-8B C3 05 0C 00 52 50 E8   &.G.*.@P.....RP.
-q

C:\TASM>


Friday 16 February 2018


CENG 222 – Computer Organization Lab Work 9

Standard I/O In 8086

Standard input output operations  are performed INT 21h interrupt in assembly.
Int 21h acts like a function. The operation to perform in interrupt no 21h is determined by the contents of the AH register.
Read the entry regarding Int 21h interrupt in “Supported Interrupt Functions” section of Emu8086 help (“help”->”Documentation and Tutorials”->“Supported Interrupt Functions”)
Pay attention to Int 21h functions where the value of AH is 1,2,9,0Ah

Exercises

1) Write an assembly program to read a character and print the same character back to screen
2) The user will enter a 2 digit decimal number write an assembly program to store the number in CX register.
3) Write an assembly program to print the English alphabet (capital letters only)
4) design an assembly function (that uses a variable for return value) that reads 5 characters from the keyboard. Call the function in main to get the characters and write the program to print the characters.


CENG 222 – Computer Organization Lab Work 8

Variables

Syntax for a variable declaration:

name DB value
name DW value

DB - stays for Define Byte.
DW - stays for Define Word.

name - can be any letter or digit combination, though it should start with a letter. It's possible to declare unnamed variables by not specifying the name (this variable will have an address but no name).

value - can be any numeric value in any supported numbering system (hexadecimal, binary, or decimal), or "?" symbol for variables that are not initialized.

Reference Address:
http://www.yecd.com/os/8086%20assembler%20tutorial%20for%20beginners%20(part%203).htm

.model small
.stack 100h
.data
VAR1 DB 5
var2 DW 1A2Bh

.code
main proc
     mov ax,@data
     mov ds,ax

     MOV AL, var1
     MOV BX, var2

     mov ax, 4c00h
     int 21h
main endp

end main

Some previous Examples to remember...

MOV AX, [1BFFH]    ; Copy 2-bytes (word) data stored at address 1BFF to AX
MOV [BX], 20h          ; Copy the word (2-bytes) data to the memory
                        ; block started at BX. (*BX = 0020h)
MOV WORD PTR [BX], 20h ; Exactly the same as above
MOV BYTE PTR [BX], 20h                ; Copy 1 byte (*BX = 20h)

Offset command

When used with a variable offset command gives the address of the variable.

MOV BX, offset var1               ; Copy address of var1 to BX
MOV DL, [offset var2]            ; Copy 1-byte data at var2 to DL

LEA (Load effective address) instruction


If we use “offset”, we need the following instruction to get the address of a data:
MOV DX, offset var1

Here, msg is defined as name of the data itself. Another way to do this without using offset:
LEA DX, var1
Both instructions above, loads the address of var1 into DX.



Experiment 1


Analyze the assembly code below:

title Register test program

.model small
.stack 100h
.data
msg db "AB",0dh,0ah,'$'

.code
main proc
     mov ax,@data
     mov ds,ax

; Add your code here
; ------------------

; ------------------

     mov ah,9
     mov dx,offset msg
     int 21h

     mov ax, 4c00h
     int 21h
main endp

end main

The program above prints “AB” to the console. The message “AB” is stored at msg in data segment. Therefore, “offset msg” is the starting address of the string. Modify the code according to the following experiments:

1-      Copy the second byte stored at “msg” (which is ‘B’ currently) to the first byte. The output should be “BB”. In order to do that, load 2-bytes data stored at “msg” into a 16-bits register. Copy one part of the register to the other part. Then, write your register back to msg.
2-      Copy the first byte stored at “msg” (which is ‘A’ currently) to the second byte. The output should be “AA”. To achieve this, load the first byte stored at “msg” into one 8-bits register (high or low part) and copy it onto the other part and write the data stored in register back to msg.
3-      Load the data stored at “msg” into one register and swap the high and low parts of the register using a temporary 8-bits register. Then write back your “swapped” register onto msg. The output should be “BA”.

Experiment 2

title Logical

.model small
.stack 100h

.data

varA db ?
varB db ?
varC db ?
varX db ?

.code

main proc

       mov ax,@data
       mov ds,ax

       ; Add your code below

main endp

end main

In the assembly code above, four variables are defined without initialized. Use debug mode to test your program and see registers’ and memory’s status after you make necessary modifications to compute some logical operations:

1 – Assign values to all variables except varX.
            varA = 10101010b
            varB = 11001100b
            varC = 11110000b

2 – Compute the following logical operations:
            a)         varX = varA OR (varB AND varC)
            b)         varX = varA OR varB OR (NOT varC)

Experiment 3

Define variables facparam and facreturn. Write an assembly function that calculates the factorial of the number stored in facparam variable and write the result to facreturn variable. The registers should remain unchanged at the end of the function call.
Use the function to calculate 3! and 5!.


CENG 222 – Computer Organization
Lab Work 7

CALL and RET instructions

CALL instruction is used for calling a subroutine/procedure/function.
Usage:

CALL <address or procedure name>

RET instruction is used for returning back to caller procedure from the called procedure. RET has no arguments. Analyze the code given in experiment.

Using stack

Stack is used for temporarily storing data to memory. It is also used by CALL and RET to store caller instruction address. Hence, it is critically important to use stack properly:
Leave stack as how you find!
It means that you need to get all the data out which you store in the stack. Do not forget any data in the stack. You can use it temporarily!

PUSH instruction stores a data in the stack.

PUSH <word data or word register>

POP instruction retrieves a data from the stack.

POP <word memory block or word register>

Examples:

PUSH AX
POP BX


EXPERIMENTS
1)
Write the following C code in assembly using no jump instructions (use loop command instead) You should not use other registers to store the value of CX temporarily. You should use the stack properly to store the value of CX properly.
Hint: Don't forget that loop command only operates on CX and to use a inner loop you must remember the value of CX for the outer loop at each iteration.

int b =0;
for(int i=0; i<5;i++)
            for(int j=0; j<10;j++)
                        b = I + 2*j;
2)
Write an assembly function that calculates factorial. Use the function to calculate 3! and 5!.

3)
Write an assembly program that calculates first 10 Fibonacci numbers. Numbers should be observed in dx consequently (since we did not learn how to print at the moment we can simulate it by observing the register in emulator).
Hint: Use the stack to reverse the order. Once you have pushed all the items, pop command will give you the last item.

CENG 222 – Computer Organization - Lab Work 6

Exercises

Re-write the following C codes in assembly.
  1. While loop
    int a = 125;
    int b = 0;
    while(a>0){
      b += a;
      a -=2;
    }
  2. While loop with a condition
    int a = 125;
    int b = 0;
    while(a>0){
      b += a;
      if(a>10)
        a -= 2;
      else
        a--;
    }
  3. For loop
    b=0;
    for(i=0;i<10;i++)
      b = b+ i*2;
    
  4. Double for loop
    b=0;
    for(i=0;i<5;i++)
      for(j=0;j<5;j++)
        b = b+ j*2;
  5. Do-while loop with a condition
    int a = 125;
    int b = 0;
    do{
      b += a;
      if(a>10)
         a -= 2;
      else
        a--;
    }while(a>0)
  6. Collatz conjecture
    int a = 21;
    while(a != 1){
      if(a%2 == 1)
        a = 3*a+1;
      else
        a = a/2;
    }

CENG 222 – Computer Organization
Lab Work 5

Jump and Labels

JMP instruction allows to “jump” into an address of a instruction in our program. You can make loops by using JMP. Usage of JMP:

JMP <address or label name>

In assembly programs, we generally use labels to mark a starting point of a code. They end with a colon sign (“:”). Example:

L1:
           
            MOV …
            SUB …
           
            JMP L1            ; Jump into the first instruction after the label “L1”.

Testing conditions and Conditional Jump

In order to test a condition, you can use SUB or AND instructions. For example you can test A = B, A > B, A >= B, A < B and A <=B conditions using SUB. You can also test the status of any bit of a register by using AND. These instructions will modify the flag bits according to the results they produce. Therefore, you can make a conditional jump using flag bits’ status. However, SUB and AND instructions also store the result into the destination register. If you just want to test the condition without modifying operands, you need to use CMP and TEST instructions. These instructions are used to modify just the flag bits.

CMP is the same as SUB but it does not modify the destination. TEST is the same as AND but it does not modify the destination. However they both modify the flag bits.
Analyze the code below:

           
            MOV CX, 0
L1:
            CMP CX, 10
            JE L2                           ; Go to L2 if CX = 10
            INC CX
            JMP L1                        ; Go to L1 (CMP instruction)
L2:
           

Some of conditional jump instructions are:

JE, JNE, JZ, JNZ, JG, JNG, JL, JNL, JLE, JGE, JNLE, JNGE

JE:       Jump if equal to
JNE:    Jump if not equal to
JZ:       Jump if zero
JNZ:    Jump if not zero
JG:      Jump if greater than
JNG:   Jump if not greater than
JL:       Jump if less than
JNL:    Jump if not less than
JLE:    Jump if less than or equal to
JGE:    Jump if greater than or equal to
JNLE: Jump if not less than nor equal to (same as JG)
JNGE: Jump if not greater than nor equal to (same as JL)

Please note that conditional jump instructions are useless alone, but if you use an instruction that modifies flag bits, before the jump instruction, you can make IF…THEN…ELSE structures. For example:

            CMP var1, var2           ; Compare var1 and var2
            JG L1                           ; Jump to L1 if var1>var2

                                            ; “IF” block
                                            ; These instructions will be executed if var1<=var2

            JMP L2                        ; End of “IF” block, we need to jump L2
L1:

                                            ; “ELSE” block
                                            ; These instructions will be executed if var1>var2

L2:                                           ; End of “IF…ELSE” block


LOOP instruction

LOOP instruction allows creating a simple loop using CX register as a counter.

Usage:
LOOP <address of label>

You need to initialize CX first for the number of repentance of the loop. Analyze the code below:

            MOV CX, 10              ; Loop 10 times
L1:
           
                                            ; Instructions in the loop
           
            LOOP L1

LOOP instruction simply decrements CX and tests if it is zero. If not zero, then jump. Otherwise, continue.

EXPERIMENTS
1.      Find the sum [1+2+3+4+5+...+10] by using both compare-jump combination and loop instruction.
2.      Find the sum [1+3+5+..+17] by using both compare-jump combination and loop instruction.
3.      Find the sum of numbers between 0 and 20 that are not divisible by 4
4.      Write a program to find the factorial of the number given in AL register. (Assume that the number is sufficiently small so that the factorial wont be larger than 32 bits)


CENG 222 – Computer Organization
Lab Work 4

Logical Operations

Some of bitwise logical operations are:
AND, OR, NOT, XOR

Bitwise logical instructions apply the operation between each corresponding bits.

Examples:
AND data, ax               ; data = data AND ax
OR dx, ax                    ; dx = dx OR ax
NOT cl                        ; cl = NOT cl
XOR ax, ax                   ; ax = ax XOR ax

Shift Operations
Shift operations basically shift the bits of the given target (given as first parameter) by a number (given by second parameter)

Some Shift Operations are:
SHL,SHR (logical/unsigned),SAL, SAR(arithmetic/signed)

See 8086 help for detailed explanation of instructions. 

Rotate Operations
Shift operations work like shift operations, they the bits of the given target (given as first parameter) by a number (given by second parameter), the shifted bits are attached to the other side of the target.

Some Rotate Operations Are:
ROL, ROR (without carry), RCL, RCR (with carry)

See 8086 help for detailed explanation of instructions.

 

EXPERIMENT 1

Given bit values
A = 10101010b
B = 11001100b
C = 11110000b

Compute the following logical operations:
  • A  AND  B
  • A OR (B   AND   C)
  • A OR B OR (NOT C)
  • (A  AND B) OR (A   AND (NOT  C))

EXPERIMENT 2

Perform the following operations without using a MUL/ IMUL or DIV/IDIV command.
Hint: When you apply a single shift operation for a number you can multiply it or divide it by 2. Shift left for multiplying, shift right for dividing.

  • 0FAAh *2
  • 0FAAh / 2
  • FFAAh  / 2
  • FFAAh * 2
  • 00AAh * 5 ( Hint: 5*x = 4*x + x)
  • 00FBh *  2 + 000Bh * 4
  • 00AAh * 18

EXPERIMENT 3

Consider using logical and shift operations for the following assignments.
Hint:
AND AL, 11111101b; command will change the second bit of AL to 0, without changing the other bits
similarly
 OR AL, 00000010b will change the second bit of AL to 1, without changing the other bits
  • Assign  AX any value. Get the 4th bit of AX to the first bit of BX. All the other bits of BX should be zero
  • Get the  3rd bit of AL to 5th bit of AH. Other bits of AH should be unchanged.
  • Get the 2nd and 3rd bits of AH to 1st and 2nd bits of AL


CENG 222 – Computer Organization
Lab Work 3

Special Register: FLAGS

The flags register is unlike the other registers on the microprocessor. The other registers hold eight or 16 bit values. The flags register is simply a collection of one bit values which help determine the current state of the processor. Although the flags register is 16 bits wide, some of them are used. Of these flags, four flags you use all the time: zero, carry, sign, and overflow. These flags are the 8086 condition codes. The flags register appears below:
Zero flag bit is set when last instruction produces 0 as a result.
Carry flag bit is set when last instruction produces a number with a carry.
Sign flag bit is set when last instruction produces a negative number.
Overflow flag bit is set when last instruction produces a number which cannot fit in register.

Flag bits are generally used for testing the conditions. For example, if you want to test whether A = B, you need to subtract A from B, then if you have a result 0, zero flag will be set. Therefore, you need to check zero flag to obtain the result of the condition.

Arithmetic operations

Some of the arithmetic operations that are supported by Intel microprocessors are:
ADD, ADC, INC, DEC, SUB, MUL, IMUL, DIV, IDIV

ADD <destination>, <addend>                    ; destination = destination + addend

ADD instruction adds <addend> to <destination>.

ADD ax, bx                ; ax = ax + bx
ADD var, 2                 ; var = var + 2
ADD al, cl                   ; al = al + cl

ADC <destination>, <addend>                    ; destination = destination + addend + C

ADC instruction adds <addend> + Carry to <destination>. It is used to add large sized numbers.
Let AX:BX be 32 bits number. Let CX:DX be another 32 bits number. In order to add these numbers together using 16-bits microprocessor, we need to use ADC.

ADD BX, DX                        ; Add low order digits
ADC AX, CX             ; Add high order digits together with carry

The instructions above simply calculates: (AX:BX) = (AX:BX) + (CX:DX)

INC <destination>                                         ; destination = destination + 1;
DEC <destination>                                       ; destination = destination - 1;

INC instruction increments and DEC instruction decrements the destination.

INC dx
DEX al
INC var


SUB <destination>, <subtrahend>              ; destination = destination – subtrahend

SUB instruction subtracts <subtrahend> from <destination>.

SUB var, 2                  ; var = var - 2;
SUB bl, cl                   ; bl = bl – cl;
SUB bx, ax                 ; bx = bx – ax;

MUL <multiplicand>

MUL instruction multiplies AX or AL register by <multiplicand>. Destination is AX and DX. If <multiplicand> is an 8-bits number, it will be multiplied by AL. The destination is AX. If <multiplicand> is a 16-bits number, it will be multiplied by AX. The destination is (DX:AX). Here are some examples to make it clear:

MUL DL                     ; AX = AL * DL
MUL BX                    ; (DX:AX) = AX * BX
MUL var                     ; AX = AL * var         (Assuming var is a byte)
MUL var                     ; (DX:AX) = AX * var           (Assuming var is a word)

IMUL is the same as MUL, but it is used for signed numbers which are stored in 2’s complement format.

DIV <divisor>

DIV instruction divides AX or (DX:AX) by <divisor>.
If <divisor> is an 8-bits number,
            AL = AX / <divisor>
            AH = AX mod <divisor>
If <divisor> is a 16-bits number,
            AX = (DX:AX) / <divisor>
            DX = (DX:AX) mod <divisor>


Here is piece of code for division.

MOV AX, 1000
MOV DL, 30
DIV DL

The code above divides 1000 by 30. The quotient (33) will be in AL. The remainder (10) will be in AH. If the number is in <divisor> is too small or zero, the quotient will be too large to fit in AL. Therefore you need to select the registers according to their sizes. For example if you want to divide 1000 by 3, you will get 333 as a quotient which cannot be stored in 8-bits register AL! In order to do this, you need to use 16-bits division. Here is how:

MOV AX, 1000          ; The number is 1000.
MOV DX, 0                ; DX = 0, so (DX:AX) is 0000:1000 = 1000.
MOV BX, 3                ; Divisor is 3
DIV BX                      ; AX = (DX:AX) / 3 = 1000 / 3 = 333. AX is capable to store 333.
                                    ; DX = (DX:AX) mod 3 = 1000 mod 3 = 1.

IDIV is the same as DIV, but it is used for signed numbers which are stored in 2’s complement format.

EXPERIMENTS Advanced Arithmetic Operations
I) Write assembly program to do the following substractions.For each value, what is the hexadecimal result? What is the decimal result? Investigate Carry and Sign and Zero Bits.
  • 01h – 01h
  • 00h + 00h
  • 265Ch - 1AFFh.
  • 0000h - 0001h

II)  Write assembly programs to find the sum of the following hexadecimal numbers. For each summation answer following questions. Does the result fit in a 16 bit register? Why? Investigate the Carry and sign bits.
  • 0Fh + 01h
  • FFFFh + 0001h
  • 0FAAh + 1B04h +  F0A0h.

III) Write assembly programs to do the following computations. Chose registers according to the data and state how the resulting number is represented.
  • 03h * 05h
  • A1A1h * 00F0h
  • (A5C4h * 0040h) + 00FFh
  • (A5C4h / 0004h) – 0A0Ah
  • 02h * AAh * F2h