Recent Posts

Pages: 1 2 [3] 4 5 ... 10
21
Programming with NASM / Re: Linux x86. call error when linking gcc
« Last post by debs3759 on April 25, 2023, 07:10:50 PM »
Thanks Fred!
Hi again Anton,
I think we may need to ask WHY you are doing some of the things you do. Maybe you have something special in mind.

Best,
Frank
What do you mean, Frank?
Writing about technical stuff?
Well... I like to write a lot! :)

Frank was asking Anton why he is doing what he is doing :)
22
Programming with NASM / Re: Linux x86. call error when linking gcc
« Last post by fredericopissarra on April 25, 2023, 06:01:10 PM »
Thanks Fred!
Hi again Anton,
I think we may need to ask WHY you are doing some of the things you do. Maybe you have something special in mind.

Best,
Frank
What do you mean, Frank?
Writing about technical stuff?
Well... I like to write a lot! :)
23
Programming with NASM / Re: Linux x86. call error when linking gcc
« Last post by Frank Kotler on April 25, 2023, 05:14:23 PM »
Thanks Fred!
Hi again Anton,
I think we may need to ask WHY you are doing some of the things you do. Maybe you have something special in mind.

Best,
Frank


24
Programming with NASM / Re: Linux x86. call error when linking gcc
« Last post by fredericopissarra on April 25, 2023, 02:27:34 PM »
The problem is simple: If you compile and take a look at the object file:
Code: [Select]
$ nasm -felf32 -o main.o main.asm
$ objdump -h main.o
Sections:
Idx Name          Siz.      VMA       LMA       File off  Algn
  0 .text         0000000b  00000000  00000000  00000160  2**4
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
  1 my_section    00000006  00000000  00000000  00000170  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA
Where's the "CODE" attribute!? This section is not "exectable". If you add this attribute to the section:
Code: [Select]
...
  section my_section exec
...
And compile again, you'll get:
Code: [Select]
$ nasm -felf32 -o main.o main.asm
$ objdump -h main.o
Sections:
Idx Name          Siz.      VMA       LMA       File off  Algn
  0 .text         0000000b  00000000  00000000  00000160  2**4
                  CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE
  1 my_section    00000006  00000000  00000000  00000170  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
$ gcc -s -m32 -o main main.o
$ ./main
$ echo $?
1

PS: Ahhhh.. I've changed the code a little:
Code: [Select]
  bits 32

  section .text

  global main

main:
  mov eax, 1
  call sum

  ; Since you are using the main() function, called from crt0.o, the
  ; end of your program is a simple return.
  ret

  section my_section exec

sum:
  ;mov ebx, 1    ; This should be avoided, since EBX should be preserved!
  ret
In general, if you are building a standalone program in assembly, avoid using external libraries like libc... But if you still want to use it, you MUST obey the ABI.

The difference:
Code: [Select]
$ nasm -felf32 -o main.o main.asm     # exit with ret
$ gcc -s -m32 main.o -o main
$ nasm -felf32 -o main2.o main2.asm      # exit with sys_exit.
$ ld -s -melf_i386 -e main main2.o -o main2

$ ls -goh main*
-rwxr-xr-x 1  14K apr 25 11:40 main
-rwxr-xr-x 1 4,5K apr 25 11:42 main2
...
$ ldd main main2
main:
        linux-gate.so.1 (0xf7f7c000)
        libc.so.6 => /lib32/libc.so.6 (0xf7d78000)
        /lib/ld-linux.so.2 (0xf7f7e000)
main2:
        Not a dynamic executable
25
Programming with NASM / Re: Linux x86. call error when linking gcc
« Last post by AntonPotapov on April 25, 2023, 12:42:01 PM »
I don't understand why exactly "mov ebx, 1" is causing the error?  why there are no errors when linking with ld?
26
Programming with NASM / Re: Linux x86. call error when linking gcc
« Last post by Frank Kotler on April 25, 2023, 02:51:28 AM »
Ignore me.

But lose the home made section.

Best,
Frank

27
Programming with NASM / Re: Linux x86. call error when linking gcc
« Last post by Frank Kotler on April 25, 2023, 01:53:45 AM »

Code: [Select]
section my_section ; read only!
mov ebx, 1 ; seg fault

I think that's your problem.

Why the "home made" section?

If you must... call it "rw"...

gdb will like it better with "-F dwarf" on Nasm's command line...

untested!

Best,
Frank

28
Programming with NASM / Linux x86. call error when linking gcc
« Last post by AntonPotapov on April 25, 2023, 12:12:47 AM »
Linux x86.
When I link with ld, everything works.
I can't understand why when linking with gcc I can't call my function from the declared my_section?
Here's my code. If executed correctly, it should return error 1
main.asm
Code: [Select]
section .text
global main

main:
mov eax, 1
call sum
int 0x80

section my_section
sum:
mov ebx, 1
ret

Code compilation and linking.
Release:
Code: [Select]
#/bin/bash
nasm -f elf main.asm
gcc -m32 -o main main.o

Debug:
Code: [Select]
#/bin/bash
nasm -f elf -Lw -g main.asm
gcc -m32 -o main main.o

When I run the code I get:
Code: [Select]
Segmentation fault (core dumped)

This is what gdb says when running the Debug version:
Code: [Select]
Single stepping until exit from function sum,
which has no line number information.

Program received signal SIGSEGV, Segmentation fault.
29
Programming with NASM / Preprocessor. Array
« Last post by AntonPotapov on April 24, 2023, 01:34:07 PM »
How to use a string as an array in the preprocessor?
For example I have a string:
Code: [Select]
%define string "Hello World"
how do i get e.g. 'W' ?
Does the preprocessor have arrays?
I want to get a string character similar to C:
Code: [Select]
char *str = "Hello World";
char ch = str[6]; // just like that

I recently started learning assembler, literally half a week ago. I read the documentation: https://www.nasm.us/xdoc/2.16.01/html/nasmdoc4.html.
I found operators like substr, which should be enough for my purposes. But I want to understand the preprocessor more thoroughly.
30
Using NASM / Re: Wrong far jump address generated by NASM 2.16.1
« Last post by fredericopissarra on April 22, 2023, 05:49:42 PM »
I'm not sure but, this bug could also affect addresses in 32/64 bit protected mode.
Probably not, because in 32/64 codes, usually, the memory model is FLAT. No need to do a jump intersegment.
Pages: 1 2 [3] 4 5 ... 10