flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > Two PE64 examples for beginners

Author
Thread Post new topic Reply to topic
frankobach26



Joined: 10 Apr 2026
Posts: 19
Location: Kassel
frankobach26 13 Oct 2026, 18:02
Two example Shows console Modus 2. Expl type in two numbers with a result and 1. Example simple messagebox with another result...

Code:

Include 'win64a.inc'

format PE64 GUI 4.0
entry main

section '.data' data readable writeable
    a           dq 365
    b           dq 2026

    title       db 'Fasm calculation',0
    prefix      db 'our days living on earth : '
    prefix_len  = $ - prefix

    message     rb 80
    numbuf      rb 32

section '.text' code readable executable

main:
    sub rsp, 8

    ; 1. calculation
    mov rax, [a]
    imul rax, [b]

    ; 2. Zahl in Dezimaltext umwandeln
    lea r10, [numbuf + 31]
    mov byte [r10], 0
    mov r8, 10

.convert:
    xor edx, edx
    div r8
    add dl, '0'
    dec r10
    mov [r10], dl
    test rax, rax
    jnz .convert

    ; Anfang und Laenge der Zahl merken
    mov r9, r10
    lea r8, [numbuf + 31]
    sub r8, r9

    ; 3. Prefix in die Meldung kopieren
    lea r10, [message]
    lea r11, [prefix]
    mov ecx, prefix_len

.copy_prefix:
    test rcx, rcx
    jz .copy_number
    mov al, [r11]
    mov [r10], al
    inc r11
    inc r10
    dec rcx
    jmp .copy_prefix

    ; 4. Zahl hinter den Prefix kopieren
.copy_number:
    mov rcx, r8

.copy_digit:
    test rcx, rcx
    jz .finish
    mov al, [r9]
    mov [r10], al
    inc r9
    inc r10
    dec rcx
    jmp .copy_digit

.finish:
    mov byte [r10], 0

    ; 5. Ergebnis anzeigen
    invoke MessageBox, NULL, message, title, MB_OK

    ; 6. Programm beenden
    invoke ExitProcess, 0

section '.idata' import data readable
    library kernel32, 'kernel32.dll', \
            user32, 'user32.dll'

include 'api/kernel32.inc'
include 'api/user32.inc'
    


Last edited by frankobach26 on 13 Oct 2026, 18:09; edited 2 times in total
Post 13 Oct 2026, 18:02
View user's profile Send private message Reply with quote
frankobach26



Joined: 10 Apr 2026
Posts: 19
Location: Kassel
frankobach26 13 Oct 2026, 18:04
Code:
include 'win64a.inc'

format PE64 console
entry main

section '.data' data readable writeable

    prompt1 db 'please type in first number: '
    prompt1_len = $ - prompt1

    prompt2 db 'please txpe in second number: '
    prompt2_len = $ - prompt2

    result_msg db 'result is : '
    result_len = $ - result_msg

    newline db 13, 10
    exit_msg db 'Enter to end..'
    exit_len = $ - exit_msg

    input1 rb 64
    input2 rb 64
    buffer rb 32
    keybuf rb 8

    written dd 0
    hOut dq 0
    hIn dq 0

section '.text' code readable executable

main:
    sub rsp, 8

    invoke GetStdHandle, -11
    mov [hOut], rax

    invoke GetStdHandle, -10
    mov [hIn], rax

    ; Erste Zahl einlesen
    invoke WriteConsoleA, [hOut], prompt1, prompt1_len, written, 0
    invoke ReadConsoleA, [hIn], input1, 64, written, 0

    ; Erste Zahl in Integer umwandeln
    lea rsi, [input1]
    xor rax, rax

.parse1:
    movzx edx, byte [rsi]
    cmp dl, '0'
    jb .first_done
    cmp dl, '9'
    ja .first_done
    imul rax, rax, 10
    sub edx, '0'
    add rax, rdx
    inc rsi
    jmp .parse1

.first_done:
    mov r12, rax

    ; Zweite Zahl einlesen
    invoke WriteConsoleA, [hOut], prompt2, prompt2_len, written, 0
    invoke ReadConsoleA, [hIn], input2, 64, written, 0

    ; Zweite Zahl in Integer umwandeln
    lea rsi, [input2]
    xor rax, rax

.parse2:
    movzx edx, byte [rsi]
    cmp dl, '0'
    jb .second_done
    cmp dl, '9'
    ja .second_done
    imul rax, rax, 10
    sub edx, '0'
    add rax, rdx
    inc rsi
    jmp .parse2

.second_done:
    mov r13, rax

    ; Multiplikation
    mov rax, r12
    imul rax, r13

    ; Ergebnis in Dezimalstring umwandeln
    lea rdi, [buffer + 31]
    mov byte [rdi], 0
    mov ebx, 10

.convert:
    xor edx, edx
    div rbx
    add dl, '0'
    dec rdi
    mov [rdi], dl
    test rax, rax
    jnz .convert

    ; Ergebnistext ausgeben
    invoke WriteConsoleA, [hOut], result_msg, result_len, written, 0

    ; Ziffernlaenge berechnen
    lea r8, [buffer + 31]
    sub r8, rdi

    ; Ergebnis ausgeben
    invoke WriteConsoleA, [hOut], rdi, r8d, written, 0

    ; Neue Zeile
    invoke WriteConsoleA, [hOut], newline, 2, written, 0

    ; Programm sichtbar offen halten
    invoke WriteConsoleA, [hOut], exit_msg, exit_len, written, 0
    invoke ReadConsoleA, [hIn], keybuf, 8, written, 0

    invoke ExitProcess, 0

section '.idata' import data readable writeable
    library kernel32, 'kernel32.dll'
    include 'api/kernel32.inc'
    ;
    
Post 13 Oct 2026, 18:04
View user's profile Send private message Reply with quote
sylware



Joined: 23 Oct 2020
Posts: 694
Location: Marseille/France
sylware 14 Oct 2026, 11:58
Is PE64 is actually PE+?
Post 14 Oct 2026, 11:58
View user's profile Send private message Reply with quote
Byte_Blast



Joined: 01 Jul 2026
Posts: 18
Location: Australia
Byte_Blast 16 Oct 2026, 04:26
sylware wrote:
Is PE64 is actually PE+?


PE64 is defined as PE32+ in microsoft documentation, you pretty much still have the correct interpretation so yes.
Post 16 Oct 2026, 04:26
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8563
Location: Kraków, Poland
Tomasz Grysztar 16 Oct 2026, 09:04
sylware wrote:
Is PE64 is actually PE+?
See https://board.flatassembler.net/topic.php?t=20690#205476
Post 16 Oct 2026, 09:04
View user's profile Send private message Visit poster's website Reply with quote
sylware



Joined: 23 Oct 2020
Posts: 694
Location: Marseille/France
sylware 16 Oct 2026, 11:29
While I am thinking abouth this, for CPU exe and dynamic lib, PE+/ELF64 are so much obsolete.

Baw.
Post 16 Oct 2026, 11:29
View user's profile Send private message Reply with quote
Core i7



Joined: 14 Nov 2024
Posts: 178
Location: Socket on motherboard
Core i7 17 Oct 2026, 08:31
frankobach26, why are you calculating the result manually? There are ready-made APIs for this wsprintf(). As it stands, it’s as if you’re working for the computer, rather than it working for you. And if this is a form, the window should be defined in the resources and invoked—using CreateWindow() for a main window, or DialogBoxParam() for a dialog window.
Code:
format  pe64 gui 4.0
entry   main
include 'win64ax.inc'
;------------
section '.data' data readable writeable
a      dq  365
b      dq  2026
buff   db  0
;------------
section '.text' code readable executable
main:  sub     rsp,8

       mov     rax,[a]
       imul    rax,[b]

       mov     ebx,1000
       xor     edx,edx
       div     ebx
       xchg    ebx,edx

      cinvoke  wsprintf,buff,<'Our days living on Earth: %d.%d',0>,eax,ebx
       invoke  MessageBox,0,buff,<'Fasm calculation',0>,0
       invoke  ExitProcess, 0
;------------
section '.idata' import data readable
library kernel32,'kernel32.dll', user32,'user32.dll'
include 'api/kernel32.inc'
include 'api/user32.inc'
    

sylware, and what’s relevant these days, if not PE+?
Post 17 Oct 2026, 08:31
View user's profile Send private message Reply with quote
sylware



Joined: 23 Oct 2020
Posts: 694
Location: Marseille/France
sylware 17 Oct 2026, 11:38
It is not the point: it is completely obsolete on modern hardware architectures, "relevant" or whatever, We have enough experience to know that now.

ELF64 is as obsolete than PE+, nowadays I use my own ultra simple format (the JSON of exe and dynamic link) which I embed in an ELF64 capsule. This is basically one log2 aligned memory segment with an entry point and that's it. For dynamic libs, you get a "userland syscall" number resolution function offset and that's it. I am still on an another project, but I plan to try to build the system interface dynamic libs with that format (the main issue being c++).

We are near zero-format, and does the job beyond good enough (actually, if "something" would require more than that, the problem is that "something").
Post 17 Oct 2026, 11:38
View user's profile Send private message Reply with quote
Core i7



Joined: 14 Nov 2024
Posts: 178
Location: Socket on motherboard
Core i7 17 Oct 2026, 12:08
Is this a custom project? Do you have any ready-made files for testing, so we can verify their binary structure?
Post 17 Oct 2026, 12:08
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2026, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.