Programming Fundamentals/Variables/Assembly

From Wikiversity
Jump to navigation Jump to search

name_x86.asm[edit]

;This program displays "Hello <name>!" based on user input.
;
;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

prompt_out: mov	eax, sys_write	    ;eax = sys_write
            mov	ebx, stdout         ;ebx = stdout
            mov	ecx, prompt         ;ecx = prompt address
            mov	edx, prompt_len     ;edx = prompt length
            int sys_call            ;call system

name_in:    mov eax, sys_read       ;eax = sys_read
            mov ebx, stdin          ;ebx = stdin
            mov ecx, name           ;ecx = name address
            mov edx, name_maxlen    ;edx = name maximum length
            int sys_call            ;call system
            sub eax, 1              ;eax = input name length - newline
            mov [name_len], eax     ;save name length

hello_out:  mov	eax, sys_write	    ;eax = sys_write
            mov	ebx, stdout         ;ebx = stdout
            mov	ecx, hello          ;ecx = prompt address
            mov	edx, hello_len      ;edx = prompt length
            int sys_call            ;call system

name_out:   mov	eax, sys_write	    ;eax = sys_write
            mov	ebx, stdout         ;ebx = stdout
            mov	ecx, name           ;ecx = prompt address
            mov	edx, [name_len]     ;edx = name length
            int sys_call            ;call system

exclam_out: mov	eax, sys_write	    ;eax = sys_write
            mov	ebx, stdout         ;ebx = stdout
            mov	ecx, exclam         ;ecx = exclamation address
            mov	edx, exclam_len     ;edx = exclamation 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_read    equ 3
sys_write   equ 4
sys_call    equ 0x80
stdin       equ 0
stdout      equ 1
linefeed    equ 0x0a
newline     db linefeed
newline_len equ $ - newline
prompt	    db 'Enter your name:', linefeed    
prompt_len  equ	$ - prompt
name_len    dd 4
hello	    db 'Hello '    
hello_len   equ	$ - hello
exclam      db '!'
exclam_len  equ $ - exclam

            section .bss
name        resb 255
name_maxlen equ $ - name

name_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

prompt_out: mov rax, sys_write      ;rax = sys_write
            mov rdi, stdout         ;rdi = stdout
            mov rsi, prompt         ;rsi = prompt address
            mov rdx, prompt_len     ;rdx = prompt length
            syscall                 ;call system
          
name_in:    mov rax, sys_read       ;rax = sys_read
            mov rdi, stdin          ;rdi = stdin
            mov rsi, name           ;rsi = name address
            mov rdx, name_maxlen    ;rdx = name maximum length
            syscall                 ;call system
            mov [name_len], rax		;save name length

hello_out:  mov rax, sys_write      ;rax = sys_write
            mov rdi, stdout         ;rdi = stdout
            mov rsi, hello          ;rsi = hello address
            mov rdx, hello_len      ;rdx = hello length
            syscall                 ;call system

name_out:   mov rax, sys_write      ;rax = sys_write
            mov rdi, stdout         ;rdi = stdout
            mov rsi, name           ;rsi = name address
            mov rdx, [name_len] 	;rdx = name length
            syscall                 ;call system

exclam_out: mov	rax, sys_write	    ;eax = sys_write
            mov rdi, stdout         ;rdi = stdout
            mov rsi, exclam         ;rsi = exclam address
            mov rdx, exclam_len     ;rdx = name 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_read    equ 0
sys_write   equ 1
sys_exit    equ 60
stdin       equ 0
stdout      equ 1
linefeed    equ 0x0a
prompt      db "Enter your name:", linefeed
prompt_len  equ $ - prompt
name_len	dq 0
hello	    db 'Hello '    
hello_len   equ	$ - hello
exclam      db '!'
exclam_len  equ $ - exclam

            section .bss
name        resb 255
name_maxlen equ $ - name

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]