Lab8

advertisement
CS 206D Computer Organization
Lab8
CS 111
Exercise 1
- Write an assembly program that ask the user to enter a positive hex
(2 digits) number and multiply the input by 2. Then print the result
in Binary form.
CS 111
.MODEL SMALL
.STACK 100H
.DATA
MSG1 DB
MSG2 DB
'ENTER A POSITIVE HEX NUMBER (2 digit) : $'
0AH,0DH,'THE RESULT IN BINARY: ','$‘
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
LEA DX, MSG1
MOV AH, 9
INT 21H
; initialize DS
; load and display the string
XOR BX,BX
; BX WILL HOLD THE INPUT
MOV AH,1
INT 21H
WHILE:
CMP AL, 0DH
; CR?
JE ENDWHILE
CMP AL,'9'
; AL>9?
JG LETTER ; Al is letter
;ELSe AL is digit
AND AL , 0FH
JMP SHIFT
LETTER:
SUB AL , 37H
SHIFT:
SHL BX,4
OR BL,AL
;Insert value to bx
INT 21H
; Read again
JMP WHILE
ENDWHILE:
LEA DX, MSG2
MOV AH, 9
INT 21H
MOV AL,BL
MOV CL , 2
; Multiply by 2
MUL CL
; The result will be stored in AX
MOV BX,AX
;Print ax in binary form
MOV CX,16
; AX is 16 bits
MOV AH,2
PRINT:
ROL BX,1
JC ONE ;IF (CF == 1)
;Else cf = 0
MOV DL, 30H
; To print 0
JMP DISPLAY
\ ONE:
MOV DL, 31H
;To print 1
DISPLAY:
INT 21H
LOOP PRINT
Addressing mode
CS 111
Register Indirect Mode
• The offset address of the operand is contained in a register. I.e. The
register acts as a pointer to the memory location.
• The register must be one of the following: BX, SI, DI. BP.
•Format: [register]
Ex. suppose that SI contains 0110h, and the word at
0110h contains 1884h.
• MOV AX, [SI]
• MOV AX, SI
CS 111
Based and Indexed Addressing Modes
• The operand’s offset address is obtained by adding a number called a
displacement to the contents of a register.
•Syntax:
[register + displacement]
[displacement + register]
[register] + displacement
displacement + [register]
displacement [register]
CS 111
Based and Indexed Addressing Modes
Ex. Suppose that ALPHA is
declared as:
ALPHA DW 0123H, 0116H, 0789H,
0ABCDH
• What will be the result
of following instructions:
In segment address by DS.
instruction
Suppose also that:
• BX contains 2
offset 0002 contains 1022 h
• SI contains 4
offset 0004contains 2DDCh
• DI contains 1
MOV AX,[ALPHA+BX]
MOV BX,[BX+2]
MOV CX,ALPHA[SI]
MOV AX,-2[SI]
MOV BX,[ALPHA+3+DI]
MOV AX,[BX]2
CS 111
NUMBER MOVED
Based and Indexed Addressing Modes
instruction
NUMBER MOVED
MOV AX,[ALPHA+BX]
0116H
MOV BX,[BX+2]
2DDCh
MOV CX,ALPHA[SI]
0789H
MOV AX,-2[SI]
1022H
MOV BX,[ALPHA+3+DI]
0789H
MOV AX,[BX]2
illegal
CS 111
Homework 6
Question 1: Write a program that Sum the values of even index of
array A. Suppose that Array A contains:
CS 111
.DATA
MSG DB 'THE SUM OF ARRAY ELEMENTS: $'
A DB 2,3,5,8,1,4
.CODE
MOV AX, @DATA ; initialize DS
MOV DS, AX
XOR SI,SI
;clear SI
XOR AX,AX
;AX holds sum
MOV CX,3
; no. of indexes in A
TOP:
ADD AL,A[SI]
;Sum= Sum + element
ADD SI,2;
to go the next index
LOOP TOP
;loop until done
MOV AH,9; display the string MSG
LEA DX, MSG
INT 21h
MOV DL,AL
MOV AH,2
INT 21H
Homework 6
CS 111
Download