View Single Post
Old 02-26-2005, 11:59 AM   #7 (permalink)
Lebell
Cracking the Whip
 
Lebell's Avatar
 
Location: Sexymama's arms...
Just for giggles and grins, here's the same thing in assembler.



----------------------------------------------------------------

Quote:
;*****************************************************************************************
STACK SEGMENT PARA STACK 'Stack'
DW 32 DUP(0)
STACK ENDS
;*****************************************************************************************
DATASEG SEGMENT PARA 'Data'


NUMA DB 40 ;first number in range to check
NUMB DB 65 ;last number in range to check
CNT1 DB 0 ;loop counter 1
CNT2 DB 0 ;loop counter 2
PCNT DB 0 ;'prime' counter for prime numbers found
PFLAG DB ? ;'prime' flag to signal that a prime number has been found
PTBL DB 14 DUP (0) ; table to store prime numbers
DISPA DB 'Number A = ','$'
DISPB DB 'Number B = ','$'
DISPNO DB 'Number of Primes found: ','$'
DISPPRIME DB 'The Prime numbers between them are: ','$'

NEGSIGN DB '

ASCVAL DB 5 DUP (' '), '$'

DATASEG ENDS
;*****************************************************************************************
CODESEG SEGMENT PARA 'Code'
MAIN PROC FAR
ASSUME SS:STACK,DSATASEG,CS:CODESEG ;assign the stack,data and code pointers
MOV AX,DATASEG ;Set the address of the
MOV DS,AX ; segment into DS (boiler plate)

MOV BH,NUMA ;initialize BH as the outer loop counter which will be ;the number to be tested for prime
;begin outer loop
L10: ;begin outer loop, from NUMA to NUMB
MOV PFLAG, 1 ;initialize the 'prime' flag to true

CMP BH,NUMB ;test for exit outer loop, is the counter > number B?
JG L40 ;if it is, then exit from outer loop and testing

;begin inner loop
MOV BL,2 ;begin inner loop, from 2 to 1/2 of number being tested
L20:
MOV AX,BH
IDIV BL ;divide the number, in BH, by the current counter value
CMP AH,00 ;test for a remainder
JNE L25 ;if there is a remainder, jump to the end of loop test
;and do NOT set the 'prime' flag to false
MOV PFLAG, 0 ;otherwise, set the 'prime' flag to false and
JMP L30 ;exit the inner loop



L25:
MOV AH,BH
SHR AH,1 ;shift right effectively divides AH by 2, dropping any remainder
CMP BL, AH ;test end of inner loop, is the loop counter, BL, > 1/2 the value of
; the number being tested (the outer loop counter)?
JG L30 ;if so, then exitout of inner loop
INC BL ;increment the inner loop counter by 1
JMP L20 ;end inner loop, jump back to beginning at L20
;end inner loop
;test for prime flag
L30:
CMP PFLAG,1 ;check the 'prime' flag. If it's still true, then store the number
JNE L35 ;jump to L35 if not true
MOV PTBL + PCNT, BH ;else, store the number in PTBL
INC PCNT ;and increment the 'prime' counter

L35:
INC BH ;increment the outer loop counter by 1
JMP L10 ;end outer loop, jump back to beginning at L10
;end outer loop

;end of testing logic
;begin printing results code


L40:

MOV AH,09H ;request for display
LEA DX, DISPA ; for display A label
INT 21H


CALL CONVERT
MOV AH,09H
LEA DX,ASCVAL

MOV AX,4C00H ; end
INT 21H ; processing





MAIN ENDP
;--------------------------------------------------------------------------------------------------
; This procedure converts a hex based number into an ASCII value
; for display purposes

CONVERT PROC NEAR
MOV CX,10
MOV AX,HVAL
LEA SI,ASCVAL ; load the first position into SI

B20: ; added - this part can do a negative
CMP AX, 0 ; test if neg
JGE B25 ; if >=0, then jump to normal processing
NEG AX ; twos comp the AX register if it is zero to get the pos equ

MOV [SI],'-' ; put a negative sign there

B25: ADD SI, 4


CMP AX, CX
JB B30

XOR DX, DX
DIV CX


OR DL,30H
MOV [SI],DL

DEC SI
JMP B20

B30:
OR AL,30H
MOV [SI],AL

RET

CONVERT ENDP
;---------------------------------------------------------------------------------------------------
; This procedure displays one character to the screen.

DISPLAY PROC NEAR
PUSHA ; preserve registers
LEA DX, ASCVAL
MOV AH,09H
INT 21H
POPA
RET
DISPLAY ENDP


;---------------------------------------------------------------------------------------------------
CODESEG ENDS
END MAIN
__________________
"Of all tyrannies, a tyranny exercised for the good of its victims may be the most oppressive. It may be better to live under robber barons than under omnipotent moral busybodies. The robber baron's cruelty may sometimes sleep, his cupidity may at some point be satiated; but those who torment us for our own good will torment us without end, for they do so with the approval of their own conscience." – C. S. Lewis

The ONLY sponsors we have are YOU!

Please Donate!
Lebell is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360