Programming Fundamentals/Introduction/Assembly

From Wikiversity
Jump to navigation Jump to search

hello_x86.asm[edit]

;This program displays "Hello world!"
;
;References:
;   https://www.tutorialspoint.com/assembly_programming/
;   https://www.tutorialspoint.com/compile_assembly_online.php
;   https://www.tutorialspoint.com/assembly_programming/assembly_system_calls.htm

            global  _start

_start:     section .text

display:    mov	eax, sys_write	    ;eax = sys_write
            mov	ebx, stdout         ;ebx = stdout
            mov	ecx, message        ;ecx = message address
            mov	edx, length         ;edx = message length
            int sys_call            ;call system

exit:       mov	eax, sys_exit	    ;eax = sys_exit
            mov ebx, 0              ;ebx = return code (0)
            int	sys_call            ;call system

            section .data
sys_exit    equ 1
sys_write   equ 4
sys_call    equ 0x80
stdout      equ 1
linefeed    equ 0x0a
message	    db 'Hello world!', linefeed    
length      equ	$ - message

hello_x64.asm[edit]

;This program displays 'Hello world!"
;
;References:
;   http://cs.lmu.edu/~ray/notes/nasmtutorial/
;   http://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/

            global _start

_start:     section .text

display:    mov rax, sys_write      ;rax = sys_write
            mov rdi, stdout         ;rdi = stdout
            mov rsi, message        ;rsi = message address
            mov rdx, length         ;rdx = message length
            syscall                 ;call system
          
exit:       mov rax, sys_exit       ;rax = sys_exit
            mov rdi, 0              ;rdi = return code (0)
            syscall                 ;call system

            section .data
sys_write   equ 1
sys_exit    equ 60
stdout      equ 1
linefeed    equ 0x10
message     db "Hello world!", linefeed
length      equ $ - message

Try It[edit]

Copy and paste the code above into one of the following free online development environments or use your own Assembly compiler / interpreter / IDE.

See Also[edit]