NASM - The Netwide Assembler

Please login or register.

Login with username, password and session length
Advanced search  

News:

Pages: 1 [2]

Author Topic: BASELIB: General Purpose Lib for Beginners  (Read 1246 times)

debs3759

  • Jr. Member
  • *
  • Offline Offline
  • Posts: 26
    • GPUZoo
Re: General Purpose Lib for Extreme Beginners
« Reply #15 on: October 05, 2016, 07:14:54 PM »

Thanks. I've downloaded it and will take a look at the code when I finish researching all the new CPUs from the last decade for my updated CPUID code :)
Logged
My graphics card database: www.gpuzoo.com

gharald

  • New Member
  • Offline Offline
  • Posts: 1
Re: BASELIB: General Purpose Lib for Beginners
« Reply #16 on: October 18, 2016, 06:20:02 PM »

Thanks a lot Sandakan, very useful code !
And very instructive too for beginners !
Logged

stressful

  • Jr. Member
  • *
  • Offline Offline
  • Posts: 54
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #17 on: November 03, 2016, 02:23:42 PM »

Thanks gharald and debs.

Although this library is not specifically designed for external access, the stack versions sbase3, either in DLL or .o formats can be conveniently accessed from within a 32-bit C program by observing these rules;

1) a routine with no return value is a "void" type. If it returns an int, it's an "int" type and so on.
2) a routine with arguments should be called with relevant and correct arguments (types and number of arguments)
3. For win32 source, you need to tag the routines names with a leading underscore (_) as shown in the code below. Don't do that for Linux source.

Below is an example on how you can view the stack frame and the current register dumps from C functions. This way, you can have an in-depth look to what's really going on with the the registers and the stack as it goes along the execution path.

Code: [Select]
/***************************************************
 Example: Calling sbase3 (stack version) from C
 Win32 : gcc -m32 this.c sbase3.dll -o this.exe
 Linux32: gcc -m32 this.c sbase3.o -o this
 On 32-bit systems, no need for -m32 switch
***************************************************/
#include <stdio.h>

extern void _dumpreg(int);
extern void _stackview(int);
int testfunc(int, int, char);

int main()
{
int x=10;

x = testfunc(3,2,'A');
//printf("x = %d\n",x);
_dumpreg(0);
}
int testfunc(int a, int b, char c)
{
int x=-2,y=-1;
_stackview(14);  //increase to view more

return b*2;  //EAX will capture this.
}

Yielding this output

Code: [Select]
00000041 |0061FF18  ;argument 3 = 'A'
00000002 |0061FF14  ;argument 2 = 2
00000003 |0061FF10  ;argument 1 = 3
00401412 |0061FF0C  ;EIP. Return address
0061FF38 |0061FF08  ;Caller's EBP
B7D13186 |0061FF04  ;C's thingies
741DD250 |0061FF00
FFFFFFFE |0061FEFC  ;local. x = -2
FFFFFFFF |0061FEF8  ;local. y = -1
0061FEE0 |0061FEF4  ;C thingies
00401C70 |0061FEF0
00000041 |0061FEEC  ;what is C doing here? This is the third argument re-appearing.
004012A0 |0061FEE8  ;C push ESI/EDI. Nobody knows why
004012A0 |0061FEE4*  ;C push ESI/EDI
EAX|00000004 EBX|00297000 ECX|00000001   ;EAX = return value. X will capture it
EDX|00000000 ESI|004012A0 EDI|004012A0
EBP|0061FF38 ESP|0061FF14 EIP|0040141A

You can test calling other routines from within C. But not all routines are suitable for such purposes and I haven't thoroughly tested them all. This is for "sbase3" only. Other binaries are not suitable for such purposes (due to different calling conventions etc).
Logged

stressful

  • Jr. Member
  • *
  • Offline Offline
  • Posts: 54
    • BASELIB
Re: General Purpose Lib for Extreme Beginners
« Reply #18 on: November 09, 2016, 05:42:40 AM »

-deleted-
« Last Edit: November 09, 2016, 06:02:04 AM by stressful »
Logged

stressful

  • Jr. Member
  • *
  • Offline Offline
  • Posts: 54
    • BASELIB
Re: General Purpose Lib for Extreme Beginners
« Reply #19 on: November 09, 2016, 05:43:45 AM »

-deleted-
« Last Edit: November 09, 2016, 06:02:38 AM by stressful »
Logged

stressful

  • Jr. Member
  • *
  • Offline Offline
  • Posts: 54
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #20 on: November 20, 2016, 08:03:39 PM »

Due to board's restrictions (I think), I can no longer update the main attachment in the first post.
As an alternative, the latest revision can be downloaded here: BASELIB
« Last Edit: November 23, 2016, 04:11:39 PM by stressful »
Logged

stressful

  • Jr. Member
  • *
  • Offline Offline
  • Posts: 54
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #21 on: December 02, 2016, 04:38:52 PM »

I uploaded the latest version (Revision 1.0.18) to my Google+ page. You can refer to the:
BASELIB Collection section or visit 
the profile for other things related to the library.

You can safely ignore all other attachments you might found on this board since I can't no longer update them due to board restrictions. But I'll post future updates announcement here.
 
Logged

Mich

  • Jr. Member
  • *
  • Offline Offline
  • Posts: 8
Re: BASELIB: General Purpose Lib for Beginners
« Reply #22 on: December 03, 2016, 03:26:04 AM »

Windows 32 bit
This is my environment
Trying out your lib
Quote
[SECTION .drectve info align=8]
DB "kernel32.dll", 0x20 ; Space Terminated Strings
DB "MSVCRT.dll", 0x20
;DB "sbase3.dll",0x20

%include "sbase32w.asm"

extern ExitProcess
section .data
hello db 'Hello World',0
fmt db"%c",0

section .code
global main

main:
 call readch      ;this will crash
 call exitx
 
;   call getchar
;   push eax
;   push fmt
;   call printf
;   add   esp,8
;   push 0
;   call ExitProcess

;\nasm\bin\NASM -f win32
;\nasm\bin\golink /console /entry main

This compiles but crashes
The quoted code works as I understand.
Mich
Logged

stressful

  • Jr. Member
  • *
  • Offline Offline
  • Posts: 54
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #23 on: December 03, 2016, 05:25:28 AM »

Hi Mich. I don't quite understand your current code setup. But for calling readch in an interactive session, you probably want to halt the screen after that.

Code: [Select]
call readch
call halt
;... the key in AL
call exitx

Similar example can be seen in my post #3 on the first page.

Regards

« Last Edit: December 03, 2016, 05:29:53 AM by stressful »
Logged

stressful

  • Jr. Member
  • *
  • Offline Offline
  • Posts: 54
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #24 on: December 11, 2016, 10:43:26 PM »

I uploaded the latest version (Revision 1.1.0) to my Google+ page. You can refer to the:

BASELIB Collection section or visit 
the profile for other things related to the library.

You can safely ignore all other attachments you might found on this board since I can't no longer update them due to board restrictions. But just in case, I am attaching the latest release here in this post (see attachment).

I don't have the plan to extend the support for BASELIB any further. If there's no more fatal bug, then take this as the final update.

Regards
« Last Edit: January 15, 2017, 07:52:55 AM by stressful »
Logged

Mich

  • Jr. Member
  • *
  • Offline Offline
  • Posts: 8
Re: BASELIB: General Purpose Lib for Beginners
« Reply #25 on: December 12, 2016, 03:48:54 AM »

I'm just a computer enthusiast and have no formal training in computer science or programming. I'm just an old guy trying to beat Alzheimer decease by trying to grasp something.

Using your lib I compiled and run the following proggie on Vista 32bit:
Code: [Select]
;--------------------------------------------
; Compile: nasm -f win32 sbase32w.asm
; Link:
; golink /console /entry main sbase32w.obj kernel32.dll msvcrt.dll
;--------------------------------------------

Extern ExitProcess
section .bss
CharFromGetChar resb 1

section .data
hello db 'Hello World',0xa,0

section .text
global Start
Start:
push hello
call prnstrz

call readch
mov [CharFromGetChar],al
push CharFromGetChar
call prnchar
call exit
;############################################
19456bytes

Quote
CPU Disasm
Address   Hex dump          Command                                  Comments
<ModuleEn /.  68 00604000   PUSH OFFSET 00406000                     ; [???
00401005  |.  E8 EE400000   CALL 004050F8         ;call prnstrz
0040100A  |.  E8 51410000   CALL 00405160                            ; /Arg1 = sbase32w.405160
0040100F  |.  A2 10604000   MOV BYTE PTR DS:[406010],AL              ; \???
00401014  |.  68 10604000   PUSH OFFSET 00406010                     ; [???
00401019  |.  E8 763F0000   CALL 00404F94                            ; [sbase32w.00404F94
0040101E  |.  E8 31600000   CALL <JMP.&msvcrt.exit>

Code: [Select]
CPU Disasm
Address   Hex dump          Command                                  Comments
004050F8  |?  55            PUSH EBP
004050F9  |?  89E5          MOV EBP,ESP
004050FB  |?  83EC 60       SUB ESP,60
004050FE  |?  83E4 F0       AND ESP,FFFFFFF0
00405101  |?  660F7F0424    MOVDQA DQWORD PTR SS:[ESP],XMM0
00405106  |?  660F7F4C24 10 MOVDQA DQWORD PTR SS:[ESP+10],XMM1
0040510C  |?  660F7F5424 20 MOVDQA DQWORD PTR SS:[ESP+20],XMM2
00405112  |?  660F7F5C24 30 MOVDQA DQWORD PTR SS:[ESP+30],XMM3
00405118  |?  660F7F6424 40 MOVDQA DQWORD PTR SS:[ESP+40],XMM4
0040511E  |?  660F7F6C24 50 MOVDQA DQWORD PTR SS:[ESP+50],XMM5
00405124  |?  50            PUSH EAX
00405125  |?  51            PUSH ECX
00405126  |.  52            PUSH EDX
00405127  |?  FF75 08       PUSH DWORD PTR SS:[EBP+8]
0040512A  |?  E8 191F0000   CALL <JMP.&msvcrt.printf>
0040512F  |.  83C4 04       ADD ESP,4
00405132  |?  5A            POP EDX
00405133  |?  59            POP ECX
00405134  |?  58            POP EAX
00405135  |?  660F6F0424    MOVDQA XMM0,DQWORD PTR SS:[ESP]
0040513A  |?  660F6F4C24 10 MOVDQA XMM1,DQWORD PTR SS:[ESP+10]
00405140  |?  660F6F5424 20 MOVDQA XMM2,DQWORD PTR SS:[ESP+20]
00405146  |?  660F6F5C24 30 MOVDQA XMM3,DQWORD PTR SS:[ESP+30]
0040514C  |?  660F6F6424 40 MOVDQA XMM4,DQWORD PTR SS:[ESP+40]
00405152  |?  660F6F6C24 50 MOVDQA XMM5,DQWORD PTR SS:[ESP+50]
00405158   ?  89EC          MOV ESP,EBP
0040515A      5D            POP EBP
0040515B      C2 0400       RETN 4

The following code produces the same output by calling msvcrt functions directly:
Code: [Select]
section .data
hello db 'Hello World',0xa,0
extern printf,putchar, getchar,exit

section .text
global Start
Start:
push hello
call printf
add esp,4
    call getchar
and eax,0xff
push eax
call putchar
add esp, 4
call exit
2048bytes

I listed only the prnstrz in the disassembly but similar code is for many of the c funcs.

I do not quite see why not just calling the functions directly? What documentation could you refer me to?
Klod
Logged

stressful

  • Jr. Member
  • *
  • Offline Offline
  • Posts: 54
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #26 on: December 12, 2016, 04:48:56 AM »

Mich,

Windows printf, getch, putchar - they all destroy xmm0-xmm5.

BASELIB is a modular library. As with any other library, the design of this library have to include and consider many other things when wrapping API functions. This is especially true in BASELIB because it contains many SSE instructions and routines that use XMM registers. If I don't save the XMM registers, then beginners may have to save the XMMs manually every time they want to use prnstrz when making SSE-related programs.

I have no documentation, but since you already know how to use the debugger, why don't you debug the printf function and see what registers got destroyed in there.

This is Windows API design issue. You don't have this issue in Linux. Hope this helps.
 
Logged

stressful

  • Jr. Member
  • *
  • Offline Offline
  • Posts: 54
    • BASELIB
Re: BASELIB: General Purpose Lib for Beginners
« Reply #27 on: December 23, 2016, 08:34:41 PM »

I am closing BASELIB with this final release (see attachment) before 2017 approaches by adding 16-bit COM source to the library. Have long abandoned it but I don't see why it should sit there in my harddrive collecting dusts and rusts. See if you can make good use of it.

You can also download BASELIB from my page (see BASELIB: COLLECTION section)

or just in case Google+ is scrapped by google, you can download it directly from my drive

Updates
13.Jan.2017
.small enhancement to stackview
.small enhancement to flags
.Corrrected header entry for prnxmm (64-bit only)
.Added scan_byte, scan_word, scan_dword, scan_qword
.Modify dumpreg output
« Last Edit: January 15, 2017, 07:53:20 AM by stressful »
Logged
Pages: 1 [2]