Advertisement
obernardovieira

Hello world (and commands to assembler)

Feb 5th, 2015
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;This is a hello world program, but seems to be really hard, and now I'm compiling in Linux x86
  2. ;and this is not by me, you can found it here
  3. ;http://en.wikipedia.org/wiki/X86_assembly_language#.22Hello_world.21.22_program_for_Linux_in_NASM_style_assembly
  4. ;I will keep shearching I try to make modifications and try to print just values and caracters
  5. ;Sorry about mistakes in last shared snippet
  6. ;In linux we need the "_start:" otherwise we will get an error
  7. ;So, keep up and sorry again
  8.  
  9.  
  10. ;
  11. ; This program runs in 32-bit protected mode.
  12. ;  build: nasm -f elf -F stabs name.asm
  13. ;  link:  ld -o name name.o
  14. ;
  15. ; In 64-bit long mode you can use 64-bit registers (e.g. rax instead of eax, rbx instead of ebx, etc.)
  16. ; Also change "-f elf " for "-f elf64" in build command.
  17. ;
  18. section .data                           ; section for initialized data
  19. str:     db 'Hello world!', 0Ah         ; message string with new-line char at the end (10 decimal)
  20. str_len: equ $ - str                    ; calcs length of string (bytes) by subtracting the str's start address
  21.                                             ; from this address ($ symbol)
  22.  
  23. section .text                           ; this is the code section
  24. global _start                           ; _start is the entry point and needs global scope to be 'seen' by the
  25.                                             ; linker --equivalent to main() in C/C++
  26. _start:                                 ; definition of _start procedure begins here
  27.     mov eax, 4                   ; specify the sys_write function code (from OS vector table)
  28.     mov ebx, 1                   ; specify file descriptor stdout --in gnu/linux, everything's treated as a file,
  29.                                              ; even hardware devices
  30.     mov ecx, str                 ; move start _address_ of string message to ecx register
  31.     mov edx, str_len             ; move length of message (in bytes)
  32.     int 80h                      ; interrupt kernel to perform the system call we just set up -
  33.                                              ; in gnu/linux services are requested through the kernel
  34.     mov eax, 1                   ; specify sys_exit function code (from OS vector table)
  35.     mov ebx, 0                   ; specify return code for OS (zero tells OS everything went fine)
  36.     int 80h                      ; interrupt kernel to perform system call (to exit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement