Jump to content

Recommended Posts

Posted
Olá!
Estou testando esse código no nasm e está dando esse erro! É um simples 'Hello Wold'
Falha de segmentação (imagem do núcleo gravada)
O código que eu estou testando é esse:
; =========================
; Programa Hello World
; =========================
; Compilação
; =========================
; nasm -f elf64 hello.asm
; ld -s -o hello hello.o
; ./hello
; =========================
; Resultado
; =========================
; Hello World
; =========================
section .data:
msg db 'Hello World', 0xa
len equ $ - msg
section .text:
global _start
_start:
mov edx, len
mov ecx, msg
mov ebx, 1
mov eax, 4
int 0x80
; Saida
mov eax, 1
mov ebx, 0
int 0x80
Quando testo os outros códigos não dá esse erro, veja:
Testei no Fedora e no Ubuntu, tudo Linux e não consegui fazer o Hello World mas quando testo os outros códigos funciona bem!
  • Curtir 1
Posted

Não é assim que se invoca syscalls em 64 bits. Tente alterar a linha de comando para compilação assim:

$ nasm -f elf32 hello.asm
$ ld -m elf_i386 -s -o hello hello.o

E verá que vai funcionar, porque o código que tu escreveu é para 32 bits mas você tá compilando para 64 bits.

  • Curtir 1
Posted

Ou use a SysV ABI:
 

;██████████████████████████████████████
;
; test.asm
;
;   $ nasm -felf64 -o test.o test.asm
;   $ ld -s -o test test.o
;
;██████████████████████████████████████

  bits  64
  default rel

;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
; section read-only
;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

  section .rodata

msg:
  db  `Hello, world!\n`
msglength equ $ - msg

;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
; section de código
;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

  section .text

  global _start
_start:
  mov   eax,1           ; sys_write
  mov   edi,eax         ; stdout
  lea   rsi,[msg]       ; Endereçamento relativo ao RIP.
  mov   edx,msglength   ; Tamanho do buffer.
  syscall

  mov   eax,60          ; sys_exit
  xor   edi,edi         ; errorcode=0
  syscall

[]s
Fred

  • Curtir 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...