12 Replies - 1247 Views - Last Post: 19 June 2021 - 12:50 PM

#1 Splu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-May 21

Nasm2GTK from 32 to 64 bits migrating

Posted 22 May 2021 - 09:28 AM

20210522
Hello anybody there,
I failed to convert this old NASM/GTK tutorial from 32 to 64 bits,(in Xubuntu 20.04.)
https://www.dreaminc...nasm-using-gtk/
I've tried static linking against Tarnyco repo:
http://www.tarnyko.net/en/?q=node/29
but now the compiler wanted the stdio.h string.h static libraries too !!
I cannot immagine where to find them or to avoid the request.
I attached my project folder tarball to this post.
Any hint ???
P.S: I cannot upload my folder tarball project (9.4 Kb).
Moderators, could You say me why?

*** nagst.asm ***

%include "gtkenums.inc"
%include "equates.inc"

global _start, main

;GTK
extern  gtk_init, gtk_window_new, gtk_button_new_with_label
extern  gtk_container_add, gtk_widget_show, gtk_main, gtk_main_quit
extern  gtk_container_set_border_width, gtk_widget_destroy
extern  gtk_window_set_position, gtk_window_set_default_size
extern  gtk_window_set_title, gtk_fixed_new, gtk_widget_set_size_request
extern  gtk_fixed_put, gtk_widget_show_all, gtk_message_dialog_new
extern  gtk_dialog_run, gtk_widget_destroy

;GLib
extern  g_signal_connect_data, g_print

SECTION .data
szPushMe            db  "Push Me!", 0
szItWorks           db  "Your first GTK window, it works!!!!!", 10 ,0
szReally            db  "Why do you want to leave me?", 0
szSad               db  "Are you sure you want to leave me?", 10
                    db  "Really?  I am sad that you are leaving me!", 10, 0
szPoof              db  "Go POOF!", 0
szTitle             db  "GTK using NASM!", 0

szevent_delete      db  "delete-event", 0
szevent_destroy     db  "destroy", 0
szevent_clicked     db  "clicked", 0  

SECTION .bss
lpBuffer    resb    24
hMain       resq    1
hButton     resq    1
hExit       resq    1
hFrame      resq    1

SECTION .text
;~ int main( int   argc,
          ;~ char *argv[] )
main:
    rep     nop
    push    rbp
    mov     rbp, rsp

    lea     rax, [rbp + 24]
    lea     rcx, [rbp + 16] 
    push    rax
    push    rcx
    call    gtk_init          ; fails here !!!
    add     rsp, 8 * 2

          ; exit to debug 20210520 
          mov       rax, 60                 ; system call for exit
          xor       rdi, rdi                ; exit code 0
          syscall



*** Makefile ***
# 20210520 nasgt.x
# NASM Calling GTK
VERSION = 0.0.1

all: comp link clean run 

comp: nasgt.asm
	nasm -felf64 nasgt.asm 

link: nasgt.o
	gcc -no-pie nasgt.o -o firstwin.x --static -lgtk-3 -lgdk-3 -lgio-2.0 -lgobject-2.0 -lglib-2.0
clean:	
	rm -f nasgt.o

run:
	./nasgt.x firstparm secondparm


Bye.

Is This A Good Question/Topic? 0
  • +

Replies To: Nasm2GTK from 32 to 64 bits migrating

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 16302
  • View blog
  • Posts: 64,852
  • Joined: 12-June 08

Re: Nasm2GTK from 32 to 64 bits migrating

Posted 22 May 2021 - 10:08 AM

Quote

P.S: I cannot upload my folder tarball project (9.4 Kb).

File uploads have been temporarily disabled.
Was This Post Helpful? 0
  • +
  • -

#3 Splu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-May 21

Re: Nasm2GTK from 32 to 64 bits migrating

Posted 22 May 2021 - 10:24 AM

I have posted the wrong Makefile, sorry:

*** Makefile ***

# 20210520 nasgt.x
# NASM Calling GTK
VERSION = 0.0.1

all: comp link clean run 

comp: nasgt.asm
	nasm -felf64 nasgt.asm 

link: nasgt.o
	gcc -no-pie nasgt.o -o firstwin.x --static /opt/gtk342_static/lib/libgtk-3.a /opt/gtk342_static/lib/libgdk-3.a /opt/gtk342_static/lib/libgio-2.0.a /opt/gtk342_static/lib/libgobject-2.0.a /opt/gtk342_static/lib/libglib-2.0.a 

clean:	
	rm -f nasgt.o

run:
	./nasgt.x firstparm secondparm


Was This Post Helpful? 0
  • +
  • -

#4 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 931
  • View blog
  • Posts: 2,376
  • Joined: 28-March 11

Re: Nasm2GTK from 32 to 64 bits migrating

Posted 22 May 2021 - 09:08 PM

Give me a few days, got an old laptop from father in law, I need to upgrade it and get my code on it. I will post a solution hopefully by Tuesday or sooner.
Was This Post Helpful? 0
  • +
  • -

#5 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 931
  • View blog
  • Posts: 2,376
  • Joined: 28-March 11

Re: Nasm2GTK from 32 to 64 bits migrating

Posted 25 May 2021 - 07:33 PM

Splu

Is there a reason you are trying to statically link?

First, converting code from x86 to x64 is not just changing the register names, and data sizes and other things... Many things have changed, the biggest being calling conventions and to top that off, Linux and Windows now use different calling conventions. Each one uses shadow space differently, passes parameters differently, gets return values differently. We don't save/push/pop rsp or rbp since we don't have/use stack pointers, instead we have what's called shadow space.

All in all, you need to learn a bunch of new stuff for x64, most important is the System V Application Binary Interface AMD64 Yes it is relevant for Intel too. There might be more current versions, I just did a quick search and grabbed that link.

Anywho... it turns out I converted this a while back, here is the code.

;~ %include "gtkenums.inc"
;~ %include "equates.inc"

;~ GtkButtonsType
GTK_BUTTONS_NONE        equ 0
GTK_BUTTONS_OK          equ 1
GTK_BUTTONS_CLOSE       equ 2
GTK_BUTTONS_CANCEL      equ 3
GTK_BUTTONS_YES_NO      equ 4
GTK_BUTTONS_OK_CANCEL   equ 5

;~ GtkDialogFlags:
;~ Flags used to influence dialog construction.
GTK_DIALOG_MODAL                equ 1 << 0
GTK_DIALOG_DESTROY_WITH_PARENT  equ 1 << 1

;~ GtkWindowPosition
GTK_WIN_POS_NONE                equ 0
GTK_WIN_POS_CENTER              equ 1
GTK_WIN_POS_MOUSE               equ 2
GTK_WIN_POS_CENTER_ALWAYS       equ 3
GTK_WIN_POS_CENTER_ON_PARENT    equ 4

;~ GtkWindowType
GTK_WINDOW_TOPLEVEL     equ 0
GTK_WINDOW_POPUP        equ 1

;~ GtkResponseType
GTK_RESPONSE_NONE               equ -1
GTK_RESPONSE_REJECT             equ -2
GTK_RESPONSE_ACCEPT             equ -3
GTK_RESPONSE_DELETE_EVENT       equ -4
GTK_RESPONSE_OK                 equ -5
GTK_RESPONSE_CANCEL             equ -6
GTK_RESPONSE_CLOSE              equ -7
GTK_RESPONSE_YES                equ -8
GTK_RESPONSE_NO                 equ -9
GTK_RESPONSE_APPLY              equ -10
GTK_RESPONSE_HELP               equ -11

;~ GtkMessageType
;~ The type of message being displayed in the dialog.
GTK_MESSAGE_INFO        equ 0
GTK_MESSAGE_WARNING     equ 1
GTK_MESSAGE_QUESTION    equ 2
GTK_MESSAGE_ERROR       equ 3
GTK_MESSAGE_OTHER       equ 4

TRUE            equ     1
FALSE           equ     0
NULL            equ     0

global  main

;GTK
extern  gtk_init, gtk_window_new, gtk_button_new_with_label
extern  gtk_container_add, gtk_widget_show, gtk_main, gtk_main_quit
extern  gtk_container_set_border_width, gtk_widget_destroy
extern  gtk_window_set_position, gtk_window_set_default_size
extern  gtk_window_set_title, gtk_fixed_new, gtk_widget_set_size_request
extern  gtk_fixed_put, gtk_widget_show_all, gtk_message_dialog_new
extern  gtk_dialog_run, gtk_widget_destroy

;GLib
extern  g_signal_connect_data, g_print

extern exit

SECTION .data
szPushMe            db  "Push Me!", 0
szReally            db  "Why do you want to leave me?", 0
szSad               db  "Are you sure you want to leave me?", 10
                    db  "Really?  I am sad that you are leaving me!", 10, 0
szPoof              db  "Go POOF!", 0
szTitle             db  "GTK using NASM!", 0
fmtstr              db  "Hello %s from %s", 0
szWorld             db  "World", 0
szevent_delete      db  "delete-event", 0
szevent_destroy     db  "destroy", 0
szevent_clicked     db  "clicked", 0  


SECTION .text
;~ int main( int   argc,
          ;~ char *argv[] )
main:

    mov     rsi, NULL
    mov     rdi, NULL
    call    gtk_init

    mov     rdi, GTK_WINDOW_TOPLEVEL
    call    gtk_window_new
    mov     r15, rax
    
    mov     rdx, 150
    mov     rsi, 300
    mov     rdi, rax
    call    gtk_window_set_default_size
    
    mov     rsi, GTK_WIN_POS_CENTER
    mov     rdi, r15
    call    gtk_window_set_position
    
    mov     rsi, szTitle
    mov     rdi, r15
    call    gtk_window_set_title
    
    call    gtk_fixed_new
    mov     r14, rax
    
    mov     rsi, rax
    mov     rdi, r15
    call    gtk_container_add
    
    ; Push me button
    mov     rdi, szPushMe
    call    gtk_button_new_with_label
    mov     r13, rax
    
    mov     rdx, 35
    mov     rsi, 80
    mov     rdi, rax
    call    gtk_widget_set_size_request
    
    mov     rcx, 20
    mov     rdx, 100
    mov     rsi, r13
    mov     rdi, r14
    call    gtk_fixed_put

    ; Exit button
    mov     rdi, szPoof
    call    gtk_button_new_with_label
    mov     r12, rax
    
    mov     rdx, 35
    mov     rsi, 80
    mov     rdi, rax
    call    gtk_widget_set_size_request
    
    mov     rcx, 90
    mov     rdx, 100
    mov     rsi, r12
    mov     rdi, r14
    call    gtk_fixed_put
    
    ; Signals
    mov     r9, NULL
    mov     r8, NULL
    mov     rcx, r15
    mov     rdx, event_delete
    mov     rsi, szevent_delete
    mov     rdi, r15
    call    g_signal_connect_data
           
    mov     r9, NULL
    mov     r8, NULL
    mov     rcx, rsp
    mov     rdx, event_clicked
    mov     rsi, szevent_clicked
    mov     rdi, r13
    call    g_signal_connect_data

    mov     r9, NULL
    mov     r8, NULL
    mov     rcx, r15
    mov     rdx, event_delete
    mov     rsi, szevent_clicked
    mov     rdi, r12
    call    g_signal_connect_data

    mov     rdi, r15
    call    gtk_widget_show_all
    
    call    gtk_main
    
    mov     rdi, 0
    call    exit
    ret

;~ gboolean event_delete( GtkWidget *widget,
                        ;~ GdkEvent  *event,
                        ;~ gpointer   data )
event_delete:   
    mov     rdi, rdx
    call    show_dialog
    cmp     eax, GTK_RESPONSE_NO
    jne     .bye
    mov     rax, TRUE
    ret
    
.bye:
    call    gtk_main_quit
    mov     rax, FALSE
    ret     
    
;~ void event_clicked( GtkWidget *widget,
                    ;~ gpointer   data )
event_clicked:
    mov     rcx, [rsi]
    cmp     rcx, 1
    jg      .HaveParam

    mov     rdx, szTitle
    mov     rsi, szWorld
    jmp     .Display
    
.HaveParam:
    mov     rdx, szTitle
    mov     rsi, [rsi + 16]                 
    
.Display:    
    mov     rdi, fmtstr
    xor     rax, rax
    call    g_print
    ret
    
;~ int show_dialog(gpointer window)
show_dialog:
    mov     r8, szSad
    mov     rcx, GTK_BUTTONS_YES_NO
    mov     rdx, GTK_MESSAGE_QUESTION
    mov     rsi, GTK_DIALOG_DESTROY_WITH_PARENT
    ;~ rdi == gpointer window
    call    gtk_message_dialog_new
    mov     rbx, rax

    mov     rsi, szReally
    mov     rdi, rax
    call    gtk_window_set_title
    
    mov     rdi, rbx
    call    gtk_dialog_run
       
    push    rax
    
    mov     rdi, rbx
    call    gtk_widget_destroy
    
    pop     rax  
    ret



And the makefile
APP=gtk-firstwin64

all: $(APP) clean

$(APP): $(APP).o
	gcc  -g -o $(APP) $(APP).o -e main `pkg-config --cflags --libs gtk+-3.0`

$(APP).o: $(APP).asm
	nasm -f elf64 $(APP).asm -F dwarf

clean:
	rm $(APP).o




Now, compare the code side by side to see what changed. Learn from it.

This post has been edited by GunnerInc: 25 May 2021 - 07:39 PM

Was This Post Helpful? 3
  • +
  • -

#6 Splu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-May 21

Re: Nasm2GTK from 32 to 64 bits migrating

Posted 26 May 2021 - 01:21 PM

Great !!
Couldn't imagine so many changes for x86_64. I'm totally beginner at assembly.
When I do not find something about in Stack Overflow I manage to use
$ gcc -S prog.c -o prog.asm
and try to learn something. Looking at the code with a mirror. (GAS|NASM)

So disappointing that Windows and Linux are moving to different protocols.
All my knowledge limits to 16 registers. 8 of them:
RSI RDI RDX RCX R8 R9
suppose to handle the first 6 parameters of any call to functions.
The exceeding parameters passed by the Stack.
The Stack be of a maximum of 2 GB in size.
The Stack growing data from its top to its bottom according to RBP RSP or
by committing pushq/popq. (care to align to 16 bytes (128 bits).
Another snaggy command getting in my way is:
repne scasb
rep nop (to commit PAUSE)
Well done. Tomorrow I will compare this new NASM code to the old one using gvim -d ...
Bye.
Was This Post Helpful? 0
  • +
  • -

#7 Splu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-May 21

Re: Nasm2GTK from 32 to 64 bits migrating

Posted 27 May 2021 - 01:02 PM

IT WORKS !!!
Was This Post Helpful? 0
  • +
  • -

#8 Splu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-May 21

Re: Nasm2GTK from 32 to 64 bits migrating

Posted 30 May 2021 - 11:34 AM

    ; Signals
    push    16      ; push amount of bytes malloc should allocate  
    call    malloc  ; call malloc
    add     rsp, 8  ; undo push

    mov     r9, NULL
    mov     r8, rax ; rax contains malloc output
    mov     rcx, r15
    mov     rdx, event_delete
    mov     rsi, szevent_delete
    mov     rdi, r15
    call    g_signal_connect_data
            
    push    16      ; push amount of bytes malloc should allocate  
    call    malloc  ; call malloc
    add     rsp, 8  ; undo push

    mov     r9, NULL
    mov     r8, rax ; rax contains malloc output
    mov     rcx, rsp
    mov     rdx, event_clicked
    mov     rsi, szevent_clicked
    mov     rdi, r13
    call    g_signal_connect_data
 
    push    16      ; push amount of bytes malloc should allocate  
    call    malloc  ; call malloc
    add     rsp, 8  ; undo push

    mov     r9, NULL
    mov     r8, rax ; rax contains malloc output
    mov     rcx, r15
    mov     rdx, event_delete
    mov     rsi, szevent_clicked
    mov     rdi, r12
    call    g_signal_connect_data


I've tried to pass a 16 byte memory space to R8 of g_signal_connect
but clicking on buttons always exits with "segmentation fault".
"clicked" signal maybe deprecated in my Xubuntu,
DevHelp mentions only "button_press_event".
Can you reproduce these issues?
Was This Post Helpful? 0
  • +
  • -

#9 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 931
  • View blog
  • Posts: 2,376
  • Joined: 28-March 11

Re: Nasm2GTK from 32 to 64 bits migrating

Posted 30 May 2021 - 12:27 PM

Tell me, what is your goal in using Assembly? You seem to just want to cut and paste code without any research. You really need to start with simple apps like hello world to get a handle on everything.

; Signals
push    16      ; push amount of bytes malloc should allocate 
call    malloc  ; call malloc
add     rsp, 8  ; undo push



Why are you passing parameters on the stack by using push? In 64bit code, we pass parameters in registers... Which registers depend on the OS and ABI you are using. Why are you also not saving the pointer returned by malloc? You need to pass that pointer to free when you are done using it! If you read about malloc, you would learn that. Since you are using GTK, instead of using malloc from C, I would keep it tidy and use g_malloc and g_free from the GLib library.
Was This Post Helpful? 0
  • +
  • -

#10 Splu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-May 21

Re: Nasm2GTK from 32 to 64 bits migrating

Posted 30 May 2021 - 01:10 PM

like so:
    ; Signals
    mov     rdi, 16   ; push amount of bytes malloc should allocate  
    call    g_malloc  ; call malloc
    mov     r8, rax   ; rax contains malloc output

    mov     r9, NULL
    mov     rcx, r15
    mov     rdx, event_delete
    mov     rsi, szevent_delete
    mov     rdi, r15
    call    g_signal_connect_data
            
    mov     rdi, 16   ; push amount of bytes malloc should allocate  
    call    g_malloc  ; call malloc
    mov     r8, rax   ; rax contains malloc output

    mov     r9, NULL
    mov     rcx, rsp
    mov     rdx, event_clicked
    mov     rsi, szevent_clicked
    mov     rdi, r13
    call    g_signal_connect_data


...but yet persists segmentation fault.
and I would prefer to save RAX into R8 among the other parameters passed to g_signal_connect_data.
Was This Post Helpful? 0
  • +
  • -

#11 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 931
  • View blog
  • Posts: 2,376
  • Joined: 28-March 11

Re: Nasm2GTK from 32 to 64 bits migrating

Posted 30 May 2021 - 05:28 PM

Quote

Hello GunnerInc,
g_signal_connect_data seems to through back data on R8,
so R8 cannot set to NULL.
It must point to a free memory location (Stack or Heap).
At the moment I haven't the skills to fix it myself.

I guess you didn't read the System V ABI I linked to. It would tell you calling convention used, stack alignment, volitile and non-volitile registers (which registers don't save their values across calls, and which do) R8 is a volitile register. So, if you need that value later on, you need to save r8 and retrieve it later. Instead of using r8, use one of the non-volatile registers.

Your seg fault comes from the fact in 64bit Linux, the stack needs to be 16 byte aligned. When your program starts, it is not aligned (main gets called and return address is on the stack) at a minimum, you need
sub rsp 8
at the beginning of your code.

It would behoove you to stop what you are doing and read the System V ABI.
Was This Post Helpful? 0
  • +
  • -

#12 Splu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-May 21

Re: Nasm2GTK from 32 to 64 bits migrating

Posted 19 June 2021 - 11:49 AM

I filled in the equivalent gcc/GTK-source-program,
then followed the instructions in (user 4829160) of:

here

and finally replaced each "endbr64" by "nop" to achieve the final NASM source code.
The binary result behaved right in every features.
Comparing the generated-Nasm to the human-Nasm seems the same code.
The main difference is that a...

   call    g_type_check_instance_cast


(DevHelp doesn't explain its purpose. If you avoid it, you will be WARNed by GTK)
precedes each:

   call    g_signal_connect_data


Other differences between the Nasm-objconv-generated and the Nasm-human-filled-in
are that the latter one uses "lea ..., [rel szString]" to recall the strings,
and that (for example)

   mov     qword [rbp-8H], rax


saves each GTK-function-output downto the Stack,
whilst the first one uses r12,r13,r14 non volatile registers.
In my computer only r12 and r13 are non volatile!!
The employment of r14 was responsible of my 'segmetation fault' when quit.
A very relevant news is that compiling with:

$ nasm ... -e main ...


inhibits to lead the main entry by:

        main:
                      nop
                      push    rbp
                      mov     rbp, rsp
                      ...
      


whilst compiling without it, obliges you to do.
Reading the AMD64 ABI the code should be aligned by 8 bytes segments,
in addiction to the usual 16 bytes data Stack alignment. But this
unexpectedly never occurred any issue to me.
Was This Post Helpful? 0
  • +
  • -

#13 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 931
  • View blog
  • Posts: 2,376
  • Joined: 28-March 11

Re: Nasm2GTK from 32 to 64 bits migrating

Posted 19 June 2021 - 12:50 PM

What are you trying to do?

Quote

g_type_check_instance_cast

Is a macro. Read here Gobject

Dev Help is not always complete. Use the GTK website.
Gnome Developer Centet
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1