120 likes | 207 Views
CS 206D Computer Organization Lab8. 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
E N D
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 '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 \
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 • What will be the result of following instructions: 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 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