Friday 16 February 2018


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!.

No comments:

Post a Comment