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