[41] | 1 | ; File name : Memory.asm
|
---|
| 2 | ; Project name : Assembly Library
|
---|
| 3 | ; Created date : 14.7.2010
|
---|
[46] | 4 | ; Last update : 1.10.2010
|
---|
[41] | 5 | ; Author : Tomi Tilli
|
---|
| 6 | ; Description : Functions for memory access.
|
---|
| 7 |
|
---|
| 8 | ; Section containing code
|
---|
| 9 | SECTION .text
|
---|
| 10 |
|
---|
| 11 | ;--------------------------------------------------------------------
|
---|
[46] | 12 | ; OPTIMIZE_STRING_OPERATION
|
---|
[41] | 13 | ; Parameters
|
---|
[46] | 14 | ; %1: Repeat instruction
|
---|
| 15 | ; %2: String instruction without size (for example MOVS and not MOVSB or MOVSW)
|
---|
| 16 | ; CX: Number of BYTEs to operate
|
---|
| 17 | ; DS:SI: Ptr to source data
|
---|
| 18 | ; ES:DI: Ptr to destination
|
---|
[41] | 19 | ; Returns:
|
---|
[46] | 20 | ; SI, DI: Updated by number of bytes operated
|
---|
| 21 | ; Corrupts registers:
|
---|
[41] | 22 | ; Nothing
|
---|
[46] | 23 | ;--------------------------------------------------------------------
|
---|
| 24 | %macro OPTIMIZE_STRING_OPERATION 2
|
---|
| 25 | push cx
|
---|
| 26 |
|
---|
| 27 | shr cx, 1 ; Operate with WORDs for performance
|
---|
| 28 | jcxz %%HandleRemainingByte
|
---|
| 29 | %1 %2w
|
---|
| 30 | %%HandleRemainingByte:
|
---|
| 31 | jnc SHORT %%OperationCompleted
|
---|
| 32 | %2b
|
---|
| 33 |
|
---|
| 34 | ALIGN JUMP_ALIGN
|
---|
| 35 | %%OperationCompleted:
|
---|
| 36 | pop cx
|
---|
| 37 | %endmacro
|
---|
| 38 |
|
---|
| 39 |
|
---|
| 40 | ;--------------------------------------------------------------------
|
---|
| 41 | ; Memory_CopyCXbytesFromDSSItoESDI
|
---|
| 42 | ; Parameters
|
---|
| 43 | ; CX: Number of bytes to copy
|
---|
| 44 | ; DS:SI: Ptr to source data
|
---|
| 45 | ; ES:DI: Ptr to destination buffer
|
---|
| 46 | ; Returns:
|
---|
| 47 | ; SI, DI: Updated by number of bytes copied
|
---|
[41] | 48 | ; Corrupts registers:
|
---|
[46] | 49 | ; Nothing
|
---|
[41] | 50 | ;--------------------------------------------------------------------
|
---|
| 51 | ALIGN JUMP_ALIGN
|
---|
[46] | 52 | Memory_CopyCXbytesFromDSSItoESDI:
|
---|
| 53 | OPTIMIZE_STRING_OPERATION rep, movs
|
---|
| 54 | ret
|
---|
[41] | 55 |
|
---|
[46] | 56 |
|
---|
[41] | 57 | ;--------------------------------------------------------------------
|
---|
[46] | 58 | ; Memory_ZeroSSBPwithSizeInCX
|
---|
[41] | 59 | ; Parameters
|
---|
[46] | 60 | ; CX: Number of bytes to zero
|
---|
[41] | 61 | ; SS:BP: Ptr to buffer to zero
|
---|
| 62 | ; Returns:
|
---|
| 63 | ; Nothing
|
---|
| 64 | ; Corrupts registers:
|
---|
| 65 | ; Nothing
|
---|
| 66 | ;--------------------------------------------------------------------
|
---|
| 67 | ALIGN JUMP_ALIGN
|
---|
[46] | 68 | Memory_ZeroSSBPwithSizeInCX:
|
---|
[41] | 69 | push ax
|
---|
| 70 |
|
---|
[46] | 71 | call Memory_ExchangeSSBPwithESDI
|
---|
| 72 | call Memory_ZeroESDIwithSizeInCX
|
---|
| 73 | call Memory_ExchangeSSBPwithESDI
|
---|
[41] | 74 |
|
---|
| 75 | pop ax
|
---|
| 76 | ret
|
---|
| 77 |
|
---|
| 78 | ;--------------------------------------------------------------------
|
---|
[46] | 79 | ; Memory_ZeroESDIwithSizeInCX
|
---|
[41] | 80 | ; Parameters
|
---|
[46] | 81 | ; CX: Number of bytes to zero
|
---|
| 82 | ; ES:DI: Ptr to destination buffer
|
---|
[41] | 83 | ; Returns:
|
---|
| 84 | ; Nothing
|
---|
| 85 | ; Corrupts registers:
|
---|
| 86 | ; AX
|
---|
| 87 | ;--------------------------------------------------------------------
|
---|
| 88 | ALIGN JUMP_ALIGN
|
---|
[46] | 89 | Memory_ZeroESDIwithSizeInCX:
|
---|
[41] | 90 | xor ax, ax
|
---|
[46] | 91 | ; Fall to Memory_StoreCXbytesFromAccumToESDI
|
---|
[41] | 92 |
|
---|
| 93 | ;--------------------------------------------------------------------
|
---|
[46] | 94 | ; Memory_StoreCXbytesFromAccumToESDI
|
---|
[41] | 95 | ; Parameters
|
---|
| 96 | ; AX: Word to use to fill buffer
|
---|
[46] | 97 | ; CX: Number of BYTEs to store
|
---|
| 98 | ; ES:DI: Ptr to destination buffer
|
---|
[41] | 99 | ; Returns:
|
---|
| 100 | ; Nothing
|
---|
| 101 | ; Corrupts registers:
|
---|
| 102 | ; Nothing
|
---|
| 103 | ;--------------------------------------------------------------------
|
---|
| 104 | ALIGN JUMP_ALIGN
|
---|
[46] | 105 | Memory_StoreCXbytesFromAccumToESDI:
|
---|
| 106 | OPTIMIZE_STRING_OPERATION rep, stos
|
---|
| 107 | sub di, cx
|
---|
[41] | 108 | ret
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | ;--------------------------------------------------------------------
|
---|
[46] | 112 | ; Memory_ExchangeSSBPwithESDI
|
---|
[41] | 113 | ; Memory_ExchangeDSSIwithESDI
|
---|
| 114 | ; Parameters
|
---|
| 115 | ; Nothing
|
---|
| 116 | ; Returns:
|
---|
[46] | 117 | ; SS:BP/DS:SI and ES:DI are exchanged.
|
---|
[41] | 118 | ; Corrupts registers:
|
---|
| 119 | ; Nothing
|
---|
| 120 | ;--------------------------------------------------------------------
|
---|
| 121 | ALIGN JUMP_ALIGN
|
---|
[46] | 122 | Memory_ExchangeSSBPwithESDI:
|
---|
| 123 | xchg bp, di
|
---|
| 124 | push ss
|
---|
| 125 | push es
|
---|
| 126 | pop ss
|
---|
| 127 | pop es
|
---|
| 128 | ret
|
---|
| 129 |
|
---|
| 130 | ALIGN JUMP_ALIGN
|
---|
[41] | 131 | Memory_ExchangeDSSIwithESDI:
|
---|
| 132 | xchg si, di
|
---|
| 133 | push ds
|
---|
| 134 | push es
|
---|
| 135 | pop ds
|
---|
| 136 | pop es
|
---|
| 137 | ret
|
---|
| 138 |
|
---|
| 139 |
|
---|
| 140 | ;--------------------------------------------------------------------
|
---|
| 141 | ; Memory_CopySSBPtoESDI
|
---|
| 142 | ; Parameters
|
---|
| 143 | ; Nothing
|
---|
| 144 | ; Returns:
|
---|
[46] | 145 | ; ES:DI: Same as SS:BP
|
---|
[41] | 146 | ; Corrupts registers:
|
---|
| 147 | ; Nothing
|
---|
| 148 | ;--------------------------------------------------------------------
|
---|
| 149 | ALIGN JUMP_ALIGN
|
---|
| 150 | Memory_CopySSBPtoESDI:
|
---|
| 151 | push ss
|
---|
| 152 | pop es
|
---|
| 153 | mov di, bp
|
---|
| 154 | ret
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 | ;--------------------------------------------------------------------
|
---|
| 158 | ; Memory_SetZFifNullPointerInDSSI
|
---|
| 159 | ; Parameters
|
---|
| 160 | ; DS:SI: Far pointer
|
---|
| 161 | ; Returns:
|
---|
| 162 | ; ZF: Set if NULL pointer in DS:SI
|
---|
| 163 | ; Corrupts registers:
|
---|
| 164 | ; Nothing
|
---|
| 165 | ;--------------------------------------------------------------------
|
---|
| 166 | ALIGN JUMP_ALIGN
|
---|
| 167 | Memory_SetZFifNullPointerInDSSI:
|
---|
| 168 | push ax
|
---|
| 169 | mov ax, ds
|
---|
| 170 | or ax, si
|
---|
| 171 | pop ax
|
---|
| 172 | ret
|
---|
| 173 |
|
---|
| 174 |
|
---|
| 175 | ;--------------------------------------------------------------------
|
---|
| 176 | ; Memory_ReserveCXbytesFromStackToDSSI
|
---|
| 177 | ; Parameters
|
---|
| 178 | ; CX: Number of bytes to reserve
|
---|
| 179 | ; Returns:
|
---|
| 180 | ; DS:SI: Ptr to reserved buffer
|
---|
| 181 | ; Corrupts registers:
|
---|
| 182 | ; AX
|
---|
| 183 | ;--------------------------------------------------------------------
|
---|
| 184 | ALIGN JUMP_ALIGN
|
---|
| 185 | Memory_ReserveCXbytesFromStackToDSSI:
|
---|
| 186 | pop ax
|
---|
| 187 | push ss
|
---|
| 188 | pop ds
|
---|
| 189 | sub sp, cx
|
---|
| 190 | mov si, sp
|
---|
| 191 | jmp ax
|
---|