Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Guessuing_Game/popAndPush.asm
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
42 lines (38 sloc)
1.21 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Example using push and pop that swaps two numbers | |
section .data | |
section .bss | |
num1 resb 1 ; declare variable to hold an ascii number | |
num2 resb 1 ; declare a variable to hold an ascii number | |
section .text | |
global _start ;must be declared for linker (ld) | |
_start: | |
call swap ; call swap | |
call quit ; call switch | |
swap : | |
mov rax, '2' ; put 2 in 64-bit register rax | |
mov rbx, '3' ; put 3 in 64-bit register rbx | |
push rax ; push content of rax on stack | |
push rbx ; push content of rbx on stack | |
pop rax ; get value from top of stack and put in rbx | |
pop rbx ; get new value from top of stack and put in rax | |
mov [num1], rax ; put value in rax in num1 | |
mov [num2],rbx ; put value in rbx in num2 | |
call display | |
ret | |
display: | |
; display the values | |
mov edx,1 ;message length | |
mov ecx, num1 ;message to write | |
mov ebx, 1 ;file descriptor (stdout) | |
mov eax, 4 ;system call number (sys_write) | |
int 0x80 ;call kernel | |
mov edx,1 ;message length | |
mov ecx, num2 ;message to write | |
mov ebx, 1 ;file descriptor (stdout) | |
mov eax, 4 ;system call number (sys_write) | |
int 0x80 ;call kernel | |
ret | |
quit: | |
; quit | |
mov eax, 1 ;system call number (sys_exit) | |
int 0x80 ;call kernel |