Recent Posts

Pages: 1 ... 8 9 [10]
91
Programming with NASM / Re: Seg Fault when calling scanf
« Last post by Frank Kotler on April 25, 2020, 05:15:04 AM »
I am not good at 64-bit code. It does have some stack alignment requirements. I think you've probably got it.

Best,
Frank

92
Programming with NASM / Re: Seg Fault when calling scanf
« Last post by pedrodemargomes on April 25, 2020, 04:50:13 AM »
I added the ret instruction and it continues to give seg fault.
I runned the program with gdb, and discovered that the seg fault occurs in scanf.
Here is the gdb output:
Code: [Select]
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a4f4d6 in char_buffer_rewind (buffer=0x7fffffffd8c8) at vfscanf.c:224
Strangely, when i push two registers onto the stack before calling scanf it works.
I think that it has something to do with stack aligment, maybe the stack must be align in 16 byte boundaries.
93
Programming with NASM / Re: Seg Fault when calling scanf
« Last post by Frank Kotler on April 25, 2020, 04:21:06 AM »
Hi again...

I am using Debian. Shouldn't make any difference.

As I said, I tried your code exactly as posted. It worked. Then I added a "ret" as the last thing on your ".text" section. I really think that should be there. No difference. Try it and see if it makes any difference for you. This is a real puzzle!

Best,
Frank

94
Programming with NASM / Re: Seg Fault when calling scanf
« Last post by pedrodemargomes on April 25, 2020, 03:29:16 AM »
That is awkward.
I am using Ubuntu in a x86-64 pc and compiling with this command line:
Code: [Select]
nasm -felf64 teste.s && gcc teste.o
Is there something wrong with it?
95
Programming with NASM / Re: Seg Fault when calling scanf
« Last post by Frank Kotler on April 25, 2020, 12:01:04 AM »
Hi Pedro,
Welcome to the forum.

Curiously, your code works for me, just as posted. You don't seem to "ret" from "main"... but it works anyway. I am confused!

Best,
Frank

96
Programming with NASM / Seg Fault when calling scanf
« Last post by pedrodemargomes on April 24, 2020, 03:16:56 PM »
This is my code:

Code: [Select]
   global main
   extern  printf
   extern  scanf
   section .text
main:
   push rbp
   mov rbp, rsp
   mov rax, 42
   push rax
   
   lea rsi, [rbp-8]
   mov rdi, formatNumScanf
   xor rax, rax
   call scanf wrt ..plt
   
   mov rax, [rbp-8]
   mov rdi, formatNumPrintf
   mov rsi, rax
   xor rax, rax
   call printf wrt ..plt
   
   mov rsp, rbp
   pop rbp
   section .rodata
formatNumPrintf:   db '%ld', 10, 0
formatNumScanf:   db '%ld', 0
It erros with a seg fault when calling scanf with [rbp-8] address, that is the top of the stack.
It is strange that if i push two registers onto the stack at the beggining, it works as expected.
What am i doing wrong?
97
Summer of Code Ideas / Re: Nasm is still quite stupid enough
« Last post by uncle Richard on April 24, 2020, 02:54:21 PM »
Looking at the endless abundance of high-level languages, one might think that assembler has long died. But this is not so. Suppose you write a library and want to collect all the strings in one object file. Even using the lowest level of all the higher levels - C, nothing will work. All C compilers are the same in this respect. I have an answer why this is happening. But for now, I will refrain from discussing.

The simplest program and assembler for it.

#define STR1 ";string_1\r\n"
#define STR2 ";string_2\r\n"
#define STR3 ";string_3\r\n"
#define STR4 ";string_4\r\n"
#define STR5 ";string_5\r\n"
#define STR6 ";string_6\r\n"
extern int printf();
void _start (void){
   printf(STR1);
   printf(STR2);
   printf(STR5);
}

section code
[global   _start]
_start:
; Line 8:   void _start (void){

L_4:
; Line 9:      printf(STR1);

   push   dword L_1
   call   printf
   pop   ecx
; Line 10:      printf(STR2);

   push   dword L_2
   call   printf
   pop   ecx
; Line 11:      printf(STR5);

   push   dword L_3
   call   printf
   pop   ecx
; Line 12:   }

L_5:
   ret
section data
section code
section data
section string
L_3:
   db   ";string_5"
   db   0dh
   db   0ah
   db   00h
L_2:
   db   ";string_2"
   db   0dh
   db   0ah
   db   00h
L_1:
   db   ";string_1"
   db   0dh
   db   0ah
   db   00h
section const

section code
[extern   printf]


We have 6 strings, and only 3 are in the object file. Let's cut the program down to the can’t.

#define STR1 ";string_1\r\n"
#define STR2 ";string_2\r\n"
#define STR3 ";string_3\r\n"
#define STR4 ";string_4\r\n"
#define STR5 ";string_5\r\n"
#define STR6 ";string_6\r\n"

section code
section data
section code
section data
section const

The object file is full zero. And now we take the assembler and by the hands, hands :)

   SECTION .data
SECTION .data

STR1:
   db   ";string_1", 0dh, 0ah, 00h
STR2:
   db   ";string_2", 0dh, 0ah, 00h
STR3:
   db   ";string_3", 0dh, 0ah, 00h
STR4:
   db   ";string_4", 0dh, 0ah, 00h
STR5:
   db   ";string_5", 0dh, 0ah, 00h
STR6:
   db   ";string_6", 0dh, 0ah, 00h
;.....
98
Programming with NASM / Re: Display result
« Last post by fredericopissarra on April 21, 2020, 07:29:12 PM »
But my program don't work.... Why ???
Hummm... don't know... may because it is wrong?
99
Summer of Code Ideas / Re: Nasm is still quite stupid enough
« Last post by uncle Richard on April 21, 2020, 05:57:34 PM »
Day two. The weather was great. The princess was bad. :) As often happens in such cases, the solution to the problem turned out to be very simple:

[section .bss]
[common num 200:4]
[common AtMeTwo 4:4]
[common AtMeToo 4:4]
[common LookAtMe 4:4]
[extern printf]
[section .rdata]
@10:
   db ' %d',10,0

Those who play AT&T with Yasm:

   .comm LookAtMe,04,4
   .comm AtMeToo,04,4
   .comm AtMeTwo,04,4
   .comm num,0310,4
Why int  num[50]; >>> .comm num,0310,4 in pcc 1.1.0 for win32? Who Knows?:)
pcc is not only :) GCC:

.comm LookAtMe,16
.comm AtMeToo,16
.comm AtMeTwo,16
.comm num,208

The Nasm object file looks exactly the same, but now it works:

   public   _start
   comm   num:byte:0c8h
   comm   AtMeTwo:byte:04h
   comm   AtMeToo:byte:04h
   comm   LookAtMe:byte:04h
   extrn   printf
.text   segment

_start:
      push   EBP
      mov   EBP,ESP
      sub   ESP,4
      mov   dword ptr @SYM32[00h],043h
      mov   dword ptr @SYM32[00h],02Dh
      mov   dword ptr @SYM32[00h],01Dh
      mov   EAX,@SYM32[00h]
      add   EAX,@SYM32[00h]
      add   EAX,@SYM32[00h]
      mov   @SYM32[00h],EAX
      push   dword ptr @SYM32[00h]
      push   offset FLAT:@10[041h]@SYM32
      call     printf@PC32
      add   ESP,8
      mov   ESP,EBP
      pop   EBP
      ret
.text   ends
.bss   segment
.bss   ends
   end

Understand why - don't even try! :) Anyway, try TCC as linker for Nasm. It's easy. Welcome Universal Student IDE https://sites.google.com/site/excelmidi/universal_student_ide/universal_student_ide_en for all compilers.
100
Code: [Select]
%include "io.inc"

    section .text
    global CMAIN
    CMAIN:
        section .text
        global _start       ;must be declared for using gcc
    _start:                     ;tell linker entry point
        mov edx, len    ;message length
        mov ecx, msg    ;message to write
        mov ebx, 1      ;file descriptor (stdout)
        mov eax, 4      ;system call number (sys_write)
        int 0x80        ;call kernel
        mov eax, 1      ;system call number (sys_exit)
        int 0x80        ;call kernel

    section .data

    msg db  'Hello, world!',0xa ;our dear string
    len equ $ - msg         ;length of our dear string
        xor eax, eax
        ret



its because the second _start: you made , this line

_start:                     ;tell linker entry point

delete it and it will work , and always read the error messages , it says (multiple definition of `_start') , cheers
Pages: 1 ... 8 9 [10]