Download - CS 206D Computer Organization Lab8
CS 206D Computer OrganizationLab8
CS 111
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.
.MODEL SMALL .STACK 100H
.DATA MSG1 DB 'ENTER A POSITIVE HEX NUMBER (2 digit) : $' MSG2 DB 0AH,0DH,'THE RESULT IN BINARY: ','$‘
.CODE MAIN PROC MOV AX, @DATA ; initialize DS MOV DS, AX LEA DX, MSG1 ; load and display the string MOV AH, 9 INT 21H
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
\
CS 111
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] + displacementdisplacement + [register]displacement [register]
CS 111
Based and Indexed Addressing Modes
Ex. Suppose that ALPHA is declared as:
ALPHA DW 0123H, 0116H, 0789H, 0ABCDH
In segment address by DS. Suppose also that: • BX contains 2 offset 0002 contains 1022 h• SI contains 4 offset 0004contains 2DDCh• DI contains 1
instruction NUMBER MOVED
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
• What will be the result of following instructions:
CS 111
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
Homework 6.DATA MSG DB 'THE SUM OF ARRAY ELEMENTS: $' A DB 2,3,5,8,1,4.CODEMOV AX, @DATA ; initialize DSMOV DS, AXXOR SI,SI ;clear SIXOR AX,AX ;AX holds sumMOV CX,3 ; no. of indexes in ATOP:ADD AL,A[SI] ;Sum= Sum + elementADD SI,2; to go the next indexLOOP TOP ;loop until doneMOV AH,9; display the string MSGLEA DX, MSGINT 21hMOV DL,ALMOV AH,2INT 21H