source: xtideuniversalbios/trunk/Configurator/Src/Libraries/math.asm@ 15

Last change on this file since 15 was 2, checked in by Tomi Tilli, 15 years ago
File size: 4.8 KB
Line 
1; File name : math.asm
2; Project name : Math library
3; Created date : 7.10.2009
4; Last update : 1.1.2010
5; Author : Tomi Tilli
6; Description : ASM library to for math related functions.
7
8;--------------- Equates -----------------------------
9
10; String library function to include
11%define USE_MATH_MULDWBYW ; Math_MulDWbyW
12%define USE_MATH_DIVDWBYW ; Math_DivDWbyW
13%define USE_MATH_REMTOTENTHS ; Math_RemToTenths
14
15
16;-------------- Private global variables -------------
17; Section containing initialized data
18;SECTION .data
19
20
21;-------------- Public functions ---------------------
22; Section containing code
23SECTION .text
24
25
26;--------------------------------------------------------------------
27; Macro to select lesser of two unsigned operands.
28;
29; MIN_U
30; Parameters:
31; %1: Operand 1
32; %2: Operand 2
33; Returns:
34; %1: Lesser unsigned operand
35; Corrupts registers:
36; Nothing
37;--------------------------------------------------------------------
38%macro MIN_U 2
39 cmp %1, %2 ; Is %1 smaller?
40 jb %%Return ; If so, return
41 mov %1, %2 ; Copy %2 to %1
42ALIGN JUMP_ALIGN
43%%Return:
44%endmacro
45
46
47;--------------------------------------------------------------------
48; Macro to select greater of two unsigned operands.
49;
50; MAX_U
51; Parameters:
52; %1: Operand 1
53; %2: Operand 2
54; Returns:
55; %1: Greater unsigned operand
56; Corrupts registers:
57; Nothing
58;--------------------------------------------------------------------
59%macro MAX_U 2
60 cmp %1, %2 ; Is %1 greater?
61 ja %%Return ; If so, return
62 mov %1, %2 ; Copy %2 to %1
63ALIGN JUMP_ALIGN
64%%Return:
65%endmacro
66
67
68;--------------------------------------------------------------------
69; Macro to select lesser and greater of two unsigned operands.
70;
71; MINMAX_U
72; Parameters:
73; %1: Operand 1
74; %2: Operand 2
75; Returns:
76; %1: Lesser unsigned operand
77; %2: Greater unsigned operand
78; Corrupts registers:
79; Nothing
80;--------------------------------------------------------------------
81%macro MINMAX_U 2
82 cmp %1, %2 ; Is %1 smaller?
83 jbe %%Return ; If so, return
84 xchg %1, %2 ; Exchange operands
85ALIGN JUMP_ALIGN
86%%Return:
87%endmacro
88
89
90;--------------------------------------------------------------------
91; DWORD * WORD multiplication.
92; Multiplies unsigned 32-bit integer by unsigned 16-bit integer.
93; Result is unsigned 32-bit integer, so overflow is possible.
94;
95; Math_MulDWbyW
96; Parameters:
97; DX:AX: 32-bit unsigned integer to multiply
98; CX: 16-bit unsigned multiplier
99; Returns:
100; DX:AX: 32-bit unsigned integer
101; Corrupts registers:
102; Nothing
103;--------------------------------------------------------------------
104%ifdef USE_MATH_MULDWBYW
105ALIGN JUMP_ALIGN
106Math_MulDWbyW:
107 jcxz .RetZero ; If CX=0, return 0
108 push bx
109 mov bx, dx ; Copy hiword to BX
110 xor dx, dx ; Zero DX for multiplication
111 mul cx ; DX:AX = AX (loword) * CX (multiplier)
112 push dx ; Push possible overflow
113 xchg ax, bx ; => AX=old hiword, BX=new loword
114 xor dx, dx ; Zero DX for division
115 mul cx ; DX:AX = AX (hiword) * CX (multiplier)
116 pop dx ; Pop possible overflow from first mul
117 add dx, ax ; Add new hiword to overflow from first mul
118 mov ax, bx ; Copy new loword to AX
119 pop bx
120 ret
121ALIGN JUMP_ALIGN
122.RetZero: ; Return 0 in DX:AX
123 xor ax, ax
124 xor dx, dx
125 ret
126%endif
127
128
129;--------------------------------------------------------------------
130; Divide a 32-bit unsigned integer so that quotient can be 32-bit.
131;
132; Math_DivDWbyW
133; Parameters:
134; DX:AX: 32-bit unsigned divident
135; CX: 16-bit unsigned divisor
136; Returns:
137; DX:AX: 32-bit unsigned quotient
138; BX: 16-bit unsigned remainder
139; Corrupts registers:
140; Nothing
141;--------------------------------------------------------------------
142%ifdef USE_MATH_DIVDWBYW
143ALIGN JUMP_ALIGN
144Math_DivDWbyW:
145 mov bx, ax
146 mov ax, dx
147 xor dx, dx
148 div cx
149 xchg ax, bx
150 div cx
151 xchg dx, bx
152 ret
153%endif
154
155
156;--------------------------------------------------------------------
157; Converts remainder to tenths.
158;
159; Math_RemToTenths
160; Parameters:
161; BX: 16-bit unsigned remainder
162; CX: 16-bit unsigned divisor used when calculated the remainder (max 2559)
163; Returns:
164; BX: Remainder converted to tenths
165; Corrupts registers:
166; Nothing
167;--------------------------------------------------------------------
168%ifdef USE_MATH_REMTOTENTHS
169ALIGN JUMP_ALIGN
170Math_RemToTenths:
171 push cx
172 push ax
173 mov ax, cx ; Copy divisor to AX
174 mov cl, 10 ; Load 10 to CL
175 div cl ; AL = Divisor divided by 10
176 inc ax ; Increment to compensate new remainder
177 xchg ax, bx ; AX = 16-bit remainder to convert
178 ; BL = Original divisor divided by 10
179 div bl ; AX = Original remainder converted to tenths
180 eMOVZX bx, al ; Copy return value to BX
181 pop ax
182 pop cx
183 ret
184%endif
Note: See TracBrowser for help on using the repository browser.