Program slides

advertisement
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
TITLE
8-bit multiplication using shifts
SHL_MLT.ASM
COMMENT |
Objective: To multiply two 8-bit unsigned numbers
using SHL rather than MUL instruction.
Input: Requests two unsigned numbers from user.
|
Output: Prints the multiplication result.
.MODEL SMALL
.STACK 100H
.DATA
input_prompt
DB 'Please input two short numbers: ',0
out_msg1
DB 'The multiplication result is: ',0
query_msg
DB 'Do you want to quit (Y/N): ',0
.CODE
INCLUDE io.mac
main
PROC
.STARTUP
read_input:
PutStr input_prompt ; request two numbers
GetInt AX
; read the first number
nwln
GetInt BX
; read the second number
nwln
Logical: 1
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
call
PutStr
PutInt
nwln
PutStr
GetCh
nwln
cmp
jne
done:
main
mult8
out_msg1
AX
; mult8 uses SHL instruction
query_msg
AL
; query user whether to terminate
; read response
AL,'Y'
read_input
; if response is not 'Y'
; repeat the loop
; otherwise, terminate program
; mult8 leaves result in AX
.EXIT
ENDP
;----------------------------------------------------------; mult8 multiplies two 8-bit unsigned numbers passed on to
; it in registers AL and BL. The 16-bit result is returned
; in AX. This procedure uses only SHL instruction to do the
; multiplication. All registers, except AX, are preserved.
;----------------------------------------------------------mult8 PROC
push
CX
; save registers
push
DX
push
SI
Logical: 2
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
xor
mov
mov
repeat1:
rol
jnc
mov
shl
add
skip1:
loop
rol
jnc
add
skip2:
mov
pop
pop
pop
ret
mult8 ENDP
END
DX,DX
; DX := 0 (keeps mult. result)
CX,7
; CX := # of shifts required
SI,AX
; save original number in SI
; multiply loop - iterates 7 times
BL,1
; test bits of number2 from left
skip1
; if 0, do nothing
AX,SI
; else, AX := number1*bit weight
AX,CL
DX,AX
; update running total in DX
repeat1
BL,1
skip2
DX,SI
AX,DX
SI
DX
CX
; test the rightmost bit of AL
; if 0, do nothing
; else, add number1
; move final result into AX
; restore registers
main
Logical: 3
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
;----------------------------------------------------------; mult8 multiplies two 8-bit unsigned numbers passed on to
; it in registers AL and BL. The 16-bit result is returned
; in AX. This procedure uses only SHL instruction to do the
; multiplication. All registers, except AX, are preserved.
; Demonstrates the use of bit instructions BSF and BTC.
;----------------------------------------------------------mult8 PROC
push
CX
; save registers
push
DX
push
SI
xor
DX,DX
; DX := 0 (keeps mult. result)
mov
SI,AX
; save original number in SI
repeat1:
bsf
CX,BX
; returns first 1 bit position in CX
jz
skip1
; if ZF=1, no 1 bit in BX - done
mov
AX,SI
; else, AX := number1*bit weight
shl
AX,CL
add
DX,AX
; update running total in DX
btc
BX,CX
; complement the bit found by BSF
jmp
repeat1
skip1:
Logical: 4
22:
23:
24:
25:
26:
27:
28:
skip1:
mult8
mov
pop
pop
pop
ret
ENDP
AX,DX
SI
DX
CX
; move final result into AX
; restore registers
Logical: 5
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
TITLE
Octal-to-binary conversion using shifts
OCT_BIN.ASM
COMMENT |
Objective: To convert an 8-bit octal number to the
binary equivalent using shift instruction.
Input: Requests an 8-bit octal number from user.
Output: Prints the decimal equivalent of the input
|
octal number.
.MODEL SMALL
.STACK 100H
.DATA
octal_number
DB 4 DUP (?) ; to store octal number
input_prompt
DB 'Please input an octal number: ',0
out_msg1
DB 'The decimal value is: ',0
query_msg
DB 'Do you want to quit (Y/N): ',0
.CODE
INCLUDE io.mac
main PROC
.STARTUP
read_input:
PutStr input_prompt
GetStr octal_number,4
nwln
; request an octal number
; read input number
Logical: 6
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
mov
call
PutStr
PutInt
nwln
PutStr
GetCh
nwln
cmp
jne
done:
main
BX,OFFSET octal_number ; pass octal # pointer
to_binary
; returns binary value in AX
out_msg1
AX
; display the result
query_msg
AL
; query user whether to terminate
; read response
AL,'Y'
read_input
; if response is not 'Y'
; read another number
; otherwise, terminate program
.EXIT
ENDP
;----------------------------------------------------------; to_binary receives a pointer to an octal number string in
; BX register and returns the binary equivalent in AL (AH is
; set to zero). Uses SHL for multiplication by 8. Preserves
; all registers, except AX.
;----------------------------------------------------------to_binary
PROC
push
BX
; save registers
push
CX
push
DX
Logical: 7
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
xor
AX,AX
; result := 0
mov
CX,3
; max. number of octal digits
repeat1:
; loop itarates a maximum of 3 times;
; but a NULL can terminates it early
mov
DL,[BX]
; read the octal digit
cmp
DL,0
; is it NULL?
je
finished
; if so, terminate loop
and
DL,0FH
; else, convert char. to numeric
shl
AL,3
; multiply by 8 and add to binary
add
AL,DL
inc
BX
; move to next octal digit
loop
repeat1
; and repeat
finished:
pop
DX
; restore registers
pop
CX
pop
BX
ret
to_binary
ENDP
END
main
Logical: 8
Download