Recent Posts

Pages: 1 ... 8 9 [10]
91
Hi  TOperatingSystem,

Welcome to the forum.

To remove the error message, remove the external reference, namely "global boot".

Best,
Frank


92
Programming with NASM / MrScrubDetectorForumRebatch389
« Last post by TOperatingSystem on September 12, 2020, 04:05:28 AM »
So, when I try to compile my code
Code: [Select]
section .boot
bits 16
global boot
boot:
mov ax, 0x2401
int 0x15

mov ax, 0x3
int 0x10

mov [disk],dl

mov ah, 0x2    ;read sectors
mov al, 6      ;sectors to read
mov ch, 0      ;cylinder idx
mov dh, 0      ;head idx
mov cl, 2      ;sector idx
mov dl, [disk] ;disk idx
mov bx, copy_target;target pointer
int 0x13
cli
lgdt [gdt_pointer]
mov eax, cr0
or eax,0x1
mov cr0, eax
mov ax, DATA_SEG
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
jmp CODE_SEG:boot2
gdt_start:
dq 0x0
gdt_code:
dw 0xFFFF
dw 0x0
db 0x0
db 10011010b
db 11001111b
db 0x0
gdt_data:
dw 0xFFFF
dw 0x0
db 0x0
db 10010010b
db 11001111b
db 0x0
gdt_end:
gdt_pointer:
dw gdt_end - gdt_start
dd gdt_start
disk:
db 0x0
CODE_SEG equ gdt_code - gdt_start
DATA_SEG equ gdt_data - gdt_start

times 510 - ($-$$) db 0
dw 0xaa55
copy_target:
bits 32
hello: db "Hello more than 512 bytes world!!",0
boot2:
mov esi,hello
mov ebx,0xb8000
.loop:
lodsb
or al,al
jz halt
or eax,0x0F00
mov word [ebx], ax
add ebx,2
jmp .loop
halt:
mov esp,kernel_stack_top
extern kmain
call kmain
cli
hlt

section .bss
align 4
kernel_stack_bottom: equ $
resb 16384 ; 16 KB
kernel_stack_top:
And whenver I compile with nasm -f bin boot.asm -o boot.bin, I get boot.asm:77: error: binary output format does not support external references
Anyway to fix this? Thanks, T.M.
93
Using NASM / Re: Need help in dealing with "undefined reference to `_printf'" error
« Last post by nohemon on September 12, 2020, 12:29:00 AM »
Hi Mathi!

Thank you! Good to hear from you. I am old and tired, but in good health.

"main" and "printf" - without underscores - are for gnu C (Linux) too. Needed for 32 bit Windows. Watcom C wants "main_"  and "printf_" etc. Go figure!

Nasm has "--prefix" and "--postfix" so we can add 'em from the command line...

Best,
Frank

I see. I'm learning Assembly and that was kind of confusing. Thank you for clearing it out.
94
Using NASM / Re: Need help in dealing with "undefined reference to `_printf'" error
« Last post by nohemon on September 12, 2020, 12:25:15 AM »
Hi nohemon,

       You are trying to compile a 32 bit example with 64 bit assembler/linker switches

If you want the 32 bit version. Your version should work , with proper assemble and linking commands.

Code: [Select]
global  _main
;;assemble with > nasm -fwin32 hello32.asm
;;Link with > gcc -m32 -mconsole hello32.obj -o hello32.exe

    extern  _printf

    section .text
_main:
   
    push    message
    call    _printf
    add     esp, 4
   
    mov eax,0  ;;return code of our program
    ret
message:
    db  'Hello, World', 10, 0



If you intended to do this in 64 bit :

You may need to go through the below links for more info.
1)https://cs.lmu.edu/~ray/notes/nasmtutorial/
especially the 'Using Nasm in Windows' section. (Nice tutorial . Author might be in this forum)

2) x64 calling convention for Windows.
https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention?view=vs-2019


1) What I noticed is that you need to lose the underscore (main instead of _main and printf instead of _printf)  when linking with C libraries in 64 bit. (I dont' have a clear explanation for this).

2) for undefined reference to WinMain : you need to use console subsystem to tell the linker that this is a console application.
Link with : gcc -m64 -mconsole hello.obj -o hello.exe   


Code: [Select]

;;64 bit version.
;;assemble with >  nasm -fwin64 hello.asm
;; link with >  gcc -m64 -mconsole hello.obj -o hello.exe
global  main
    extern  printf

    section .text
main:
;---------
    push rbp
    mov rbp, rsp
    ;; reserve for local variables
    ;; 2 qwords for argc and argv
    ;; and 1 qword for return value
    ;; 1 more qword to align with 16 bits
    sub rsp, 8*4   

    mov rcx,   message
    call    printf
   
    mov rax,0
    add rsp,8*4    ;; deallocate local variables
    pop rbp
    ret
;---------
message:
    db  'Hello, World', 13, 10, 0

The 64-bit version works perfectly, thanks! Although this solves my problem, I would also like to understand why the 32-bit version still doesn't work.

Using again the original code:

Code: [Select]
    extern  _printf

    section .text
_main:
   
    push    message
    call    _printf
    add     esp, 4
   
    mov eax,0  ;;return code of our program
    ret
message:
    db  'Hello, World', 10, 0

I run nasm -fwin32 Source.asm && gcc -m32 -mconsole Source.obj -o output.exe which introduces me this errors:

C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/libmingw32.a when searching for -lmingw32

This also applies for -lgcc, -lgcc_eh, -lmoldname and many more libraries. I searched this on google and these errors/warnings indicate that I'm trying to link 32-bit libraries with 64-bit linker, which is incompatible.
There is a 32-bit version of mingw, but the mingw 64-bit version also supports 32-bit. So, it's not about the linker (?).
I tried to use the link.exe, but then I get LINK : fatal error LNK1561: entry point must be defined. I add a "global main" line at the begining, re-link, and get error LNK2001: unresolved external symbol for _printf and _mainCRTStartup.

I can't think anything else.


95
Using NASM / Re: Need help in dealing with "undefined reference to `_printf'" error
« Last post by Frank Kotler on September 11, 2020, 08:57:00 PM »
Hi Mathi!

Thank you! Good to hear from you. I am old and tired, but in good health.

"main" and "printf" - without underscores - are for gnu C (Linux) too. Needed for 32 bit Windows. Watcom C wants "main_"  and "printf_" etc. Go figure!

Nasm has "--prefix" and "--postfix" so we can add 'em from the command line...

Best,
Frank

96
Example Code / Operate with integer + floating point numbers
« Last post by Ferran on September 11, 2020, 04:44:21 PM »
I had worry triying how to solve an important issue and finally i did.

In assembly code nothing is obvious, you can't add easly an integer with a floating pointed number as if, or signed and unsigned ones. All its needed to explicit it in the code.

My intention was to do this (in C language)...

Code: [Select]
#include <stdio.h>

/* ---- data section ---- */

    int i = 100;            // We choose an integer to test
    double d = -87.66;      // really we'll a substraction
    double l;
    double res = 0;

/* .... text section ---- */

void main() {

l = i;                      // Convert INT i to DOUBLE l
res = l + d;                // 'add' l +d and move it to res

/* ---- display res ---- */

    printf("Result is:  %g\n", res);    // res = 12.34
}

... but in assembly code. Today i resolved this issue fine and i want to share with you:

Code: [Select]
;-------------------------------------------------------
; itof.asm
;  Conversion INT to DOUBLE number & diplay it
; ------------------------------------------------------
; Assembly with GNU/Linux x80_86) & NASM compiler & GCC
; ------------------------------------------------------
; 1) Compile:  nasm -f elf64 itof.asm
; 2) Link: gcc -no-pie itof.o -o itof
; 3) Run:./itof
; ------------------------------------------------------

bits 64

extern printf

section .data
    i: dq 100                        ; we'll test an int to add to a double
    d: dq -87.66                     ; really we'll rest d minus d
    l: dq 0.0                        ; l will become in the double value of i
    fmt: db "Result is: %g", 10, 0

section .bss
    res: resq   1                    ; 64 bits for res

section .text
global main

main:
push rbp
   
.addition:                           ; int i --> float l ; res=l+d;
        fild        dword [i]      ; conversion from an INT to DOUBLE \o/
fst     qword [l]        ; store the floating point (no pop)
fadd     qword [d]        ; floating add l (to st0)
fstp     qword [res]      ; store into res (pop flt pt stack)

.display:

        mov rdi, fmt             ; address of format string
movq xmm0, qword [res]       ; res score
mov rax, 1               ; 1 argument to print
        call    printf               ; calling printf
 
pop rbp
       
.exit:  mov rax, 0
ret

NOTE: if you wants to do a substraccion, a multiplication or a division you need to change "fadd" by "fsub", "fmul" or "fdiv"

Enjoy it.
97
Using NASM / Re: Need help in dealing with "undefined reference to `_printf'" error
« Last post by Mathi on September 11, 2020, 03:10:44 PM »
BTW

@Frank ,
        How are you ?  :)

98
Using NASM / Re: Need help in dealing with "undefined reference to `_printf'" error
« Last post by Mathi on September 11, 2020, 03:09:49 PM »
Hi nohemon,

       You are trying to compile a 32 bit example with 64 bit assembler/linker switches

If you want the 32 bit version. Your version should work , with proper assemble and linking commands.

Code: [Select]
global  _main
;;assemble with > nasm -fwin32 hello32.asm
;;Link with > gcc -m32 -mconsole hello32.obj -o hello32.exe

    extern  _printf

    section .text
_main:
   
    push    message
    call    _printf
    add     esp, 4
   
    mov eax,0  ;;return code of our program
    ret
message:
    db  'Hello, World', 10, 0



If you intended to do this in 64 bit :

You may need to go through the below links for more info.
1)https://cs.lmu.edu/~ray/notes/nasmtutorial/
especially the 'Using Nasm in Windows' section. (Nice tutorial . Author might be in this forum)

2) x64 calling convention for Windows.
https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention?view=vs-2019


1) What I noticed is that you need to lose the underscore (main instead of _main and printf instead of _printf)  when linking with C libraries in 64 bit. (I dont' have a clear explanation for this).

2) for undefined reference to WinMain : you need to use console subsystem to tell the linker that this is a console application.
Link with : gcc -m64 -mconsole hello.obj -o hello.exe   


Code: [Select]

;;64 bit version.
;;assemble with >  nasm -fwin64 hello.asm
;; link with >  gcc -m64 -mconsole hello.obj -o hello.exe
global  main
    extern  printf

    section .text
main:
;---------
    push rbp
    mov rbp, rsp
    ;; reserve for local variables
    ;; 2 qwords for argc and argv
    ;; and 1 qword for return value
    ;; 1 more qword to align with 16 bits
    sub rsp, 8*4   

    mov rcx,   message
    call    printf
   
    mov rax,0
    add rsp,8*4    ;; deallocate local variables
    pop rbp
    ret
;---------
message:
    db  'Hello, World', 13, 10, 0

99
Using NASM / Re: Need help in dealing with "undefined reference to `_printf'" error
« Last post by Frank Kotler on September 11, 2020, 02:51:24 AM »
Hi nohemon,

Welcome to the forum.

I can't help you much with this. I haven't run Windows since win98.

However... what you show is 32 bit code. Assemble/link it as 32 bit or change it to 64 bit. I hope a Windows user will help you!

Best,
Frank

100
Using NASM / Need help in dealing with "undefined reference to `_printf'" error
« Last post by nohemon on September 09, 2020, 10:51:10 PM »
This is my code:

Code: [Select]
    global  _main
    extern  _printf

    section .text
_main:
    push    message
    call    _printf
    add     esp, 4
    ret
message:
    db  'Hello, World', 10, 0

I use nasm and gcc for compilation. I'm on a Windows 10 64-bit system.
I run nasm -f win64 source.asm. The .obj file is created with no errors.
Then I run gcc -m64 source.obj -o source.exe and I get these errors:

source.obj:source.asm:(.text+0x6): undefined reference to `_printf'

and

C:/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'

What am I missing?

Pages: 1 ... 8 9 [10]