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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
|
%ifndef _help_tool_inc
%define _help_tool_inc
; this file define the help macros for the nasm assembers. Using those macros
; can moduler program in the assember, do not need to jmp any more.
; --have fun!!
;
; Copyright 2000 Christopher Li <chrisl@turbolinux.com.cn>
;
%assign __cont__ 0
%assign __break__ 0
%assign __return__ 0
%assign __level__ 0
%assign __cndblock__ 0
%macro cat_define 3
%define %{1}%{2} %{3}
%endmacro
%macro l_define 4
%define %{1}%{2} %{3}%{4}
%endmacro
%macro cat_undef 3
%undef %{1}%{2}%{3}
%endmacro
%macro cat_lable 2
%{1}%{2}:
%endmacro
%macro linecat 3
%1 %{2}%{3}
%endmacro
%macro cat_jc 3
j%+1 %{2}%{3}
%endmacro
%macro cat_jnc 3
j%-1 %{2}%{3}
%endmacro
%macro cat_jc_near 3
j%+1 near %{2}%{3}
%endmacro
%macro cat_jnc_near 3
j%-1 near %{2}%{3}
%endmacro
%macro cat_jmp 2
jmp short %{1}%{2}
%endmacro
%macro cat_jmp_near 2
jmp near %{1}%{2}
%endmacro
%macro jmp_near 2
%if %1 > 0
jmp near %2
%else
jmp short %2
%endif
%endmacro
%macro con_push 1
%assign __lastlevel__ __level__
%assign __level__ __level__ + 1
%if __return__ > 0
%assign con_return 1
%else
%assign con_return 0
%endif
%if __cont__ > 0
%assign con_cont 1
%else
%assign con_cont 0
%endif
%if __break__ > 0
%assign con_break 1
%else
%assign con_break 0
%endif
; if the contex are going to provide the lable, then we don't provide it twice.
%ifidn %1,proc
%assign con_return 0
%elifidn %1,forcx
%assign con_break 0
%elifidn %1,repeat
%assign con_cont 0
%assign con_break 0
%elifidn %1,while
%assign con_cont 0
%assign con_break 0
%endif
%if __cndblock__ == 1
l_define %$ifnot,__lastlevel__,%$ifnot,{}
%endif
%push %1
%if con_return
l_define %$return,__level__,%$$return,__lastlevel__
%endif
%if con_break
l_define %$break,__level__,%$$break,__lastlevel__
%endif
%if con_cont
l_define %$cont,__level__,%$$cont,__lastlevel__
%endif
%ifidn %1,andblock
l_define %$ifnot,__level__,%$$ifnot,__lastlevel__
%elifidn %1,orblock
l_define %$ifnot,__level__,%$$ifnot,__lastlevel__
%endif
%endmacro
%macro con_pop 0
%pop
%assign __level__ __level__ - 1
%endmacro
%macro proc 1-*
GLOBAL %{1}
%{1}:
con_push proc
%assign __return__ __return__ + 1
%assign __have_frame__ 0
%assign %$have_local 0
%assign %$have_save 0
%assign __save_all__ 0
%assign %$local_args 0
%assign %$proc_args 0
%assign __save_args__ 0
%if %0 > 1
%assign args %0 - 1
%assign %$bp_off 4
%assign i 0
%rotate 1
%ifidn %1,withlocal
%assign %$have_local 1
%assign args args -1
%rotate 1
%endif
%assign %$proc_args args
%rep args
; define the args
%%inputarg__%{1} EQU %$bp_off
%undef .%1
%define .%{1} %%inputarg__%{1}+bp
cat_define %$proc_argv ,i,%1
%assign %$bp_off %$bp_off + 2
%assign i i+1
%rotate 1
%endrep
%if %$have_local==0
enter 0,0
%assign __have_frame__ 1
%endif
%endif
%endmacro
%macro local 0-*
%ifctx proc
%if %$have_local == 1
%assign argc %0
%assign local_vars 0
%assign %$have_local 2
%assign local_off 0
%assign i 0
%rep argc
%ifnum %2
%assign local_off local_off+%2
%assign token_eat 2
%else
%assign local_off local_off+2
%assign token_eat 1
%endif
%%local__%{1} EQU local_off
%undef .%{1}
%define .%{1} bp-%%local__%{1}
cat_define %$proc_argv ,i,%1
%assign argc argc-token_eat
%if argc <= 0
%exitrep
%endif
%assign i i+1
%rotate token_eat
%endrep
%assign %$local_args i
enter local_off,0
%assign __have_frame__ 1
%else
%error "`proc' not declear `withlocal' as the first argument"
%endif
%else
%error "expected `proc' before `local'"
%endif
%endmacro
%macro invoke 1-*
%if %0 == 2
push word %1
%assign rargc 1
%else
%rotate -2
%assign argc %0-1
%assign rargc 0
%rep argc
%ifidn %1,byte
push byte %2
%rotate -1
%assign argc argc -1
%assign rargc rargc +1
%elifidn %1,word
push word %2
%rotate -1
%assign argc argc -1
%assign rargc rargc +1
%elifidn %1,dword
push dword %2
%rotate -1
%assign argc argc -1
%assign rargc rargc +2
%else
push word %2
%assign rargc rargc +1
%endif
%rotate -1
%assign argc argc -1
%if argc <=0
%exitrep
%endif
%endrep
%rotate 1
%endif
call %1
add sp,(rargc)*2
%endmacro
%macro save 1-*
%ifctx proc
%if %$have_local == 1
%error "`save' must after `local'"
%endif
%ifidn all,%1
%assign __save_all__ 1
%if %0 > 1
%ifidn frame,%2
%if __have_frame__ == 0
enter 0,0
%assign __have_frame__ 1
%assign local_off 0
%endif
%endif
%endif
pusha
%if __have_frame__ == 1
%undef __AX
%undef __BX
%undef __CX
%undef __DX
%undef __SI
%undef __DI
%idefine __AX bp-local_off - 2
%idefine __CX bp-local_off - 4
%idefine __DX bp-local_off - 6
%idefine __BX bp-local_off - 8
; %idefine __SP bp-local_off - 10
; %idefine __BP bp-local_off - 12
%idefine __SI bp-local_off - 14
%idefine __DI bp-local_off - 16
%endif
%elifidn alld,%1
%assign __save_all__ 2
%if %0 > 1
%ifidn frame,%2
%if __have_frame__ == 0
enter 0,0
%assign __have_frame__ 1
%assign local_off 0
%endif
%endif
%endif
pushad
%if __have_frame__ == 1
%undef __EAX
%undef __EBX
%undef __ECX
%undef __EDX
%undef __ESI
%undef __EDI
%idefine __EAX bp-local_off - 4
%idefine __ECX bp-local_off - 8
%idefine __EDX bp-local_off - 12
%idefine __EBX bp-local_off - 16
; %idefine __SP bp-local_off - 10
; %idefine __BP bp-local_off - 12
%idefine __ESI bp-local_off - 18
%idefine __EDI bp-local_off - 32
%endif
%else
%assign __save_args__ %0
%assign __save_all__ 0
%assign i %0
%rep %0
push %1
cat_define %$save_argv ,i,%1
%rotate 1
%assign i i - 1
%endrep
%endif
%else
%error "`save' must after `proc'"
%endif
%endmacro
%macro endp 0-1
%ifnctx proc
%error "expected `proc' before `endp'"
%endif
cat_lable %$return,__level__
%if __save_all__ ==1
popa
%elif __save_all__ == 2
popad
%elif __save_args__ > 0
%assign i 1
%rep __save_args__
linecat pop, %$save_argv,i
%assign i i+1
%endrep
%endif
%if __have_frame__ > 0
leave
%endif
; cleanup proc_args
%assign i 0
%rep %$proc_args
; %undef %$proc_argv1
%assign i i+1
%endrep
; cleanup local_args
%assign i 0
%rep %$local_args
; cat_undef %$local_argv,i
%assign i i+1
%endrep
ret
con_pop
%assign __return__ __return__ - 1
%endmacro
%macro mov_ax 1
%if %1 == 0
sub ax,ax
%else
mov ax,%1
%endif
%endmacro
%macro return 0-*
%if %0 > 0
%assign __near__ 0
%assign argc %0
%ifidn %1,near
%assign __near__ 1
%assign argc argc-1
%rotate 1
%endif
%if argc > 0
%ifidn %1,if
; return if, {cmp ax, 0},e
%rotate 1
%rep argc-2
%1
%rotate 1
%endrep
%if __near__ == 0
cat_jc %1,%$return,__level__
%else
cat_jc_near %1,%$return,__level__
%endif
%else
%error "expect `if' after `return'"
%endif
%else
%if __near__ == 0
cat_jmp %$return,__level__
%else
cat_jmp_near %$return,__level__
%endif
%endif
%else
%if __have_frame__ == 0 && __save_all__ == 0 && __save_args__ == 0
ret
%else
cat_jmp %$return,__level__
%endif
%endif
%endmacro
%macro forcx 0-1
con_push forcx
%assign __break__ __break__ + 1
%if %0 > 0
mov cx, %1
%endif
%$begin:
%endmacro
%macro endforcx 0-*
%ifnctx forcx
%error "expected `forcx' before `endfor'"
%endif
%if %0 > 0
%ifidn %1,while
%rotate 1
cat_lable %$cont,__level__
%rep %0 - 2
%1
%rotate 1
%endrep
%else
%error "expected `while' after `endfor'"
%endif
loop%+1 %$begin
%else
loop %$begin
%endif
%$end:
cat_lable %$break,__level__
con_pop
%assign __break__ __break__ - 1
%endmacro
; forcx 5, e
; mov cx,ax
; cmp ax,bx
; endfor
;
%macro repeat 0
con_push repeat
%assign __break__ __break__ + 1
%assign __cont__ __cont__ + 1
%$begin:
cat_lable %$cont,__level__
%endmacro
%macro until 1-*
%assign __near__ 0
%assign __argc__ %0
%ifidn near,%1
%assign __near__ 1
%rotate 1
%assign __argc__ __argc__ -1
%endif
%rep __argc__ -1
%1
%rotate 1
%endrep
%ifctx repeat
%if __near__ >0
j%-1 near %$begin
%else
j%-1 %$begin
%endif
%$end:
cat_lable %$break,__level__
%else
%error "expected `repeat' before `until'"
%endif
con_pop
%assign __break__ __break__ - 1
%assign __cont__ __cont__ - 1
%endmacro
; repeat
; mov cx,ax
; cmp ax,bx
; until e
;
%macro while 0-*
con_push while
%assign __break__ __break__ + 1
%assign __cont__ __cont__ + 1
%$begin:
cat_lable %$cont,__level__
%if %0 > 0
%rep %0 -1
%1
%rotate 1
%endrep
j%-1 %$end
%endif
%endmacro
%macro endwhile 0
%ifctx while
jmp %$begin
%$end:
cat_lable %$break,__level__
%else
%error "expected `while' before `endwhile'"
%endif
con_pop
%assign __break__ __break__ + 1
%assign __cont__ __cont__ + 1
%endmacro
; while {cmp ax, bx}, e
; mov ax, cx
; endwhile
%macro break 0-*
%if %0 > 0
%ifidn %1,if
%rotate 1
%rep %0 - 2
%1
%rotate 1
%endrep
cat_jc %1,%$break,__level__
%else
%error "expect `if' after `break'"
%endif
%else
cat_jmp %$break,__level__
%endif
%endmacro
%macro continue 0-*
%if %0 > 0
%ifidn %1,if
%rotate 1
%rep %0 - 2
%1
%rotate 1
%endrep
cat_jc %1,%$cont,__level__
%else
%error "expect `if' after `continue'"
%endif
%else
cat_jmp %$cont,__level__
%endif
%endmacro
%macro goto 1-*
%if %0 > 1
%ifidn %2,if
%rotate 1
%rep %0 - 3
%2
%rotate 1
%endrep
%rotate 1
j%+1 %2
%else
%error "expect `if' after `continue'"
%endif
%else
jmp %1
%endif
%endmacro
%macro goton 1-*
%if %0 > 1
%ifidn %2,if
%rotate 1
%rep %0 - 3
%2
%rotate 1
%endrep
%rotate 1
j%+1 NEAR %2
%else
%error "expect `if' after `continue'"
%endif
%else
jmp %1
%endif
%endmacro
%macro if 0-*
%rep %0 -1
%1
%rotate 1
%endrep
con_push if
%if %0 > 0
j%-1 %$ifnot
%endif
%assign %$elif_level 0
%endmacro
%macro andblock 0
%assign __cndblock__ __cndblock__ + 1
con_push andblock
%endmacro
%macro orblock 0
%assign __cndblock__ __cndblock__ + 1
con_push orblock
%endmacro
%macro endblock 0-1
%ifctx orblock
cat_jmp %$ifnot,__level__
%$iftrue:
%elifctx andblock
%else
%error "not in andblock or orblock"
%endif
con_pop
%assign __cndblock__ __cndblock__ - 1
%endmacro
%macro condiction 1-*
%rep %0 -1
%1
%rotate 1
%endrep
%ifctx andblock
cat_jnc %1,%$ifnot,__level__
%elifctx orblock
j%+1 %$iftrue
%else
%error "not in the andblock or orblock"
%endif
%endmacro
%macro else 0-1
%assign __near__ 0
%if %0 >0
%ifidn %1 near
%assign __near__ 1
%endif
%endif
%ifctx if
%repl else
jmp_near __near__,%$ifend
%$ifnot:
%elifctx elif
; %assign elselevel %$elif_level - 1
jmp_near __near__,%$ifend
%$ifnot:
%repl else
; con_pop
; %rep elselevel
; %$ifend:
; con_pop
; %endrep
%else
%error "expected `if' before `else'"
%endif
%endmacro
%macro elif 1-*
%ifctx elif
%assign level %$elif_level + 1
%else
%assign level 1
%endif
%repl else
jmp %$ifend
%$ifnot:
%rep %0 -1
%1
%rotate 1
%endrep
con_push elif
j%-1 %$ifnot
%assign %$elif_level level
%endmacro
%macro endif 0
%assign elselevel 0
%ifctx if
%$ifnot:
con_pop
%elifctx else
%assign elselevel %$elif_level
%$ifend:
con_pop
%elifctx elif
%assign elselevel %$elif_level
%$ifnot:
con_pop
%else
%error "expected `if' or `else' before `endif'"
%endif
%rep elselevel
%$ifend:
con_pop
%endrep
%endmacro
; cmp ax,bx
; if ae
; cmp bx,cx
; if ae
; mov ax,cx
; else
; mov ax,bx
; endif
; else
; cmp ax,cx
; if ae
; mov ax,cx
; endif
; endif
%push __base__
%endif
|