Friday 16 February 2018


CENG 222 – Computer Organization
Lab Work 2

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

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;

EXP1 – Direct Addressing and Arithmetic Operations

1.      Convert your year of birth to a hexadecimal number and store write it to memory address 0200h. Is 1 byte enough to store the number? Why/why not? Do the writing to memory accordingly.
2.      Get the last 4 digits of your student number and write it to next memory address. Consider which memory address you should be using according to the answer of the first question.
3.      Find the sum of these two numbers. Store the result in register DX.
4.      Subtract first number from second store the result in register CX.

EXP2 – Indirect Addressing and Arithmetic Operations

1.      Store 50 (decimal) in memory address 0300h, this time use indirect addressing (access with [BX] ). Is 1 byte enough to store the number? Why/why not? Do the writing to memory accordingly.
2.      Store 51  in next address. Consider which memory address you should be using according to the answer of the first question.
3.      Store 52  in next address.
4.      Find the sum of 3 numbers. Store the result in the next adress.

EXP3 – Advanced Data Transfer

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 (Try both MOV and LEA commands) :

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

Advanced usage of MOV in MASM


Previously we used “offset” to point a memory location, but MASM supports a more user friendly way. As explained in previous lab, MASM does not support a moving operation to a memory location directly pointed by an immediate data but actually, currently used Intel microprocessors support it! Fortunately, some advanced usage of MOV instructions in MASM make it possible. Analyze the source and assembled code below:

Source Code
Assembled code
title Register test program

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

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

       mov al,msg
       mov msg+1,al

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

       mov ax, 4c00h
       int 21h
main endp

end main









3BA6:0000 B8A73B    MOV AX,3BA7
3BA6:0003 8ED8      MOV DS,AX

3BA6:0005 A00800    MOV AL,BYTE PTR [0008]
3BA6:0008 A20900    MOV BYTE PTR [0009],AL

3BA6:000B B409      MOV AH,09
3BA6:000D BA0800    MOV DX,0008
3BA6:0010 CD21      INT 21

3BA6:0012 B8004C    MOV AX,4C00
3BA6:0015 CD21      INT  21

Since “msg” is defined in data segment, if we use it without “offset”, it denotes the data pointed by msg (not the address of it). As we can see in the assembled code the address is inside [] brackets. ([0008]). Note that if we use “msg+1”, it means that the data stored in the memory location msg+1 that is [0009]. Here it is clear that MASM automatically calculates offsets and uses [] to point the data.
You can choose to use “offset” and denote pointer operations by yourself or use MASM to interpret the instructions. It is important to learn the usage of offset in order to understand how the operation is done exactly. Please do not go any further without understanding the code above.

LEA (Load effective address) instruction


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

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

No comments:

Post a Comment