NASM Forum > Example Code

BASELIB: General Purpose Lib for Beginners

<< < (2/10) > >>

stressful:
Good luck and be register-safe!  ;D

stressful:
Before I forgot, here's one very basic way to use "base6.dll" for windows


--- Code: ---;Example of accessing "base6.dll" from thisfile.asm
;Compile: nasm -f win64 thisfile.asm
;Link   : golink /console /entry _start thisfile.obj base6.dll
;----------------------
global _start

extern _dumpreg
extern _prnstrz
extern _exitx

section .data
s db 'Hello World',0ah,0

section .text
_start:
        mov     rax,s
        call    _prnstrz
        push    0
        call    _dumpreg
        call    _exitx
--- End code ---

Not much different from accessing other DLL files. You need a linker like GoLink btw.

stressful:
Added and update a few routines.

Revision 2.7 (Sept 18th, 2016)

--- Code: ---[+] rand - get one unsigned random int of range 0 to N
[+] randq- Generate unique random integers (unsigned) of range 0 to N-1
           and save to an array
[-] rndigit - deleted
[+] int2str
[+] file_copy
--- End code ---

stressful:
I understand that it is a great pain to explain to beginners on basic things like converting an integer to string and anything similar. Without any helper tool (debugger is too much for a 'helper' at beginners level), explanations of things like registers, instructions and their effects can be very time-consuming. One such example is converting 1234 to string "1234" by using repetitive divisions of 10 by using DIV instruction and setup

Example of extracting every digit out of 1234 and convert each digit to their corresponding ASCII characters;
1234/10     = 123, modulo or digit 4  ==> add 0x30, we get char '4' or ASCII 0x34
123/10       = 12, modulo or digit 3  ==> add 0x30, we get char '3' or ASCII 0x33
12/10        = 1, modulo or digit 2 ==> add 0x30, we get char '2' or ASCII 0x32
1/10          = 0, modulo or digit 1 ==> add 0x30, we get char '1' or ASCII 0x31

Which can be done this way for one round digit pass, getting digits from the back

--- Code: ---;'Confident' mode
xor edx,edx     ;cleared before DIV
mov eax,1234    ;the digit to convert
mov ecx,10
div ecx     ;answer in EAX, modulo in EDX (e,g 1234/10 ==> EAX = 123, EDX = 4)
add dl,0x30 ;convert digit in EDX to ASCII '4'
--- End code ---

That's the easy part. The difficult part is the Explanation, and for some beginners, the really need some visual cues to answer their famous 'what-the-hell-is-the instructor-is-talking-about' question. Luckily that can be partially solved by using the routines like "dumpreg", "halt" and "prnchr" where codes like the above can be visually and interactively turned from 'eeww' format into an instructional material to create a more dynamic and interactive teaching and learning setting where helper routines can be inserted at any point of execution. This can greatly saves time and enhance effectiveness.


--- Code: ---        ;'Instructional' mode
        mov     eax,1234;Value to convert
        mov     ecx,10  ;Divisor
        mov     edx,0   ;Clear before every DIV

        push    1       ;View all involved registers as unsigned int
        call    dumpreg ;watch ECX,EDX and EAX in initial state
        call    halt    ;Pause to observe

        div     ecx     ;Perform DIV. EAX=answer EDX=modulo (remainder)

        push    1       ;view dump as unsigned int
        call    dumpreg ;See changes in EDX and EAX. EDX = 4
        call    halt    ;pause to observe

        add     dl,30h  ;convert digit 4 to ASCII character '4'
        push    0       ;Now view in HEX format
        call    dumpreg ;EDX now becomes ASCII '4'(0x34) no more digit 4
        call    halt
--- End code ---

Producing this progressive and interactive output, by using the  "halt" and "dumpreg" combo to explain the code behavior at almost every point of instructions.

First dumpreg output:

--- Code: ---EAX|0000001234 EBX|0002781184 ECX|0000000010
EDX|0000000000 ESI|0004198400 EDI|0004198400
EBP|0000917396 ESP|0000917380 EIP|0004198415
--- End code ---

Second dumpreg output:

--- Code: ---EAX|0000000123 EBX|0002781184 ECX|0000000010
EDX|0000000004 ESI|0004198400 EDI|0004198400
EBP|0000917396 ESP|0000917380 EIP|0004198429
--- End code ---

Last dumpreg output

--- Code: ---EAX|0000007B EBX|002A7000 ECX|0000000A
EDX|00000034 ESI|00401000 EDI|00401000
EBP|000DFF94 ESP|000DFF84 EIP|0040102C
--- End code ---

After a user is satisfied that instructions are all in order and behave as they are intended, the routines can be deleted and the beginners can put their code back to 'confident' mode. LOL ;D
 

Frank Kotler:
Great explaination, stressful! The only thing I would stress is the necessity of zeroing edx before the "div" every time!

Best,
Frank

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version