Table of Contents
hello world
hello.asm
section .data
msgdb "hello, world!",`\n`
section .text
global_start
_start:
;; write syscall
movrax, 1
;; file descriptor, standard output
movrdi, 1
;; message address
movrsi, msg
;; length of message
movrdx, 14
;; call write syscall
syscall
;; exit
movrax, 60
movrdi, 0
syscall
makefile
all:
nasm -f elf64 -o hello.o hello.asm
ld -o hello hello.o
clean:
rm hello hello.o
float
circle_fpu_87c.c
#include
extern int printResult(double result);
int printResult(double result) {
printf("Circle radius is - %f\n", result);
return 0;
}
circle_fpu_87.asm
extern printResult
section .data
radiusdq 1.7
resultdq 0
SYS_EXITequ 60
EXIT_CODEequ 0
section .text
global_start
_start:
fldqword [radius]
fldqword [radius]
fmul
fldpi
fmul
fstpqword [result]
movrax, 0
movqxmm0, [result]
callprintResult
movrax, SYS_EXIT
movrdi, EXIT_CODE
syscall
makefile
build:
gcc -g -c circle_fpu_87c.c -o c.o
nasm -f elf64 circle_fpu_87.asm -o circle_fpu_87.o
ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lc circle_fpu_87.o c.o -o testFloat1
clean:
rm -rf *.o
rm -rf testFloat1
stack
stack.asm
section .data
SYS_WRITEequ 1
STD_INequ 1
SYS_EXITequ 60
EXIT_CODEequ 0
NEW_LINEdb 0xa
WRONG_ARGCdb "Must be two command line argument", 0xa
section .text
global_start
_start:
;; rcx - argc
poprcx
;;
;; Check argc
;;
cmprcx, 3
jneargcError
;;
;; start to sum arguments
;;
;; skip argv[0] - program name
addrsp, 8
;; get argv[1]
poprsi
;; convert argv[1] str to int
callstr_to_int
;; put first num to r10
movr10, rax
;; get argv[2]
poprsi
;; convert argv[2] str to int
callstr_to_int
;; put second num to r10
movr11, rax
;; sum it
addr10, r11
;;
;; Convert to string
;;
movrax, r10
;; number counter
xorr12, r12
;; convert to string
jmpint_to_str
;;
;; Print argc error
;;
argcError:
;; sys_write syscall
movrax, 1
;; file descritor, standard output
movrdi, 1
;; message address
movrsi, WRONG_ARGC
;; length of message
movrdx, 34
;; call write syscall
syscall
;; exit from program
jmpexit
;;
;; Convert int to string
;;
int_to_str:
;; reminder from division
movrdx, 0
;; base
movrbx, 10
;; rax = rax / 10
divrbx
;; add \0
addrdx, 48
addrdx, 0x0
;; push reminder to stack
pushrdx
;; go next
incr12
;; check factor with 0
cmprax, 0x0
;; loop again
jneint_to_str
;; print result
jmpprint
;;
;; Convert string to int
;;
str_to_int:
;; accumulator
xorrax, rax
;; base for multiplication
movrcx, 10
next:
;; check that it is end of string
cmp[rsi], byte 0
;; return int
jereturn_str
;; mov current char to bl
movbl, [rsi]
;; get number
subbl, 48
;; rax = rax * 10
mulrcx
;; ax = ax + digit
addrax, rbx
;; get next number
incrsi
;; again
jmpnext
return_str:
ret
;;
;; Print number
;;
print:
;;;; calculate number length
movrax, 1
mulr12
movr12, 8
mulr12
movrdx, rax
;;;;
;;;; print sum
movrax, SYS_WRITE
movrdi, STD_IN
movrsi, rsp
;; call sys_write
syscall
;;
;; newline
jmpprintNewline
;;
;; Print number
;;
printNewline:
movrax, SYS_WRITE
movrdi, STD_IN
movrsi, NEW_LINE
movrdx, 1
syscall
jmpexit
;;
;; Exit from program
;;
exit:
;; syscall number
movrax, SYS_EXIT
;; exit code
movrdi, EXIT_CODE
;; call sys_exit
syscall
makefile
all:
nasm -f elf64 -o stack.o stack.asm
ld -o stack stack.o
clean:
rm stack stack.o
string
reverse.asm
;;
;; initialized data
;;
section .data
SYS_WRITEequ 1
STD_OUTequ 1
SYS_EXITequ 60
EXIT_CODEequ 0
NEW_LINEdb 0xa
INPUTdb "Hello world!"
;;
;; non initialized data
;;
section .bss
OUTPUTresb 1
;;
;; code
;;
section .text
global_start
;;
;; main routine
;;
_start:
;; get addres of INPUT
movrsi, INPUT
;; zeroize rcx for counter
xorrcx, rcx
; df = 0 si++
cld
; remember place after function call
movrdi, $ + 15
;; get string lengt
callcalculateStrLength
;; write zeros to rax
xorrax, rax
;; additional counter for reverseStr
xorrdi, rdi
;; reverse string
jmpreverseStr
;;
;; calculate length of string
;;
calculateStrLength:
;; check is it end of string
cmpbyte [rsi], 0
;; if yes exit from function
jeexitFromRoutine
;; load byte from rsi to al and inc rsi
lodsb
;; push symbol to stack
pushrax
;; increase counter
incrcx
;; loop again
jmpcalculateStrLength
;;
;; back to _start
;;
exitFromRoutine:
;; push return addres to stack again
pushrdi
;; return to _start
ret
;;
;; reverse string
;;
;; 31 in stack
reverseStr:
;; check is it end of string
cmprcx, 0
;; if yes print result string
jeprintResult
;; get symbol from stack
poprax
;; write it to output buffer
mov[OUTPUT + rdi], rax
;; decrease length counter
decrcx
;; increase additional length counter (for write syscall)
incrdi
;; loop again
jmpreverseStr
;;
;; Print result string
;;
printResult:
movrdx, rdi
movrax, 1
movrdi, 1
movrsi, OUTPUT
syscall
jmpprintNewLine
;;
;; Print new line
;;
printNewLine:
movrax, SYS_WRITE
movrdi, STD_OUT
movrsi, NEW_LINE
movrdx, 1
syscall
jmpexit
;;
;; Exit from program
;;
exit:
;; syscall number
movrax, SYS_EXIT
;; exit code
movrdi, EXIT_CODE
;; call sys_exit
syscall
makefile
all:
nasm -g -f elf64 -o reverse.o reverse.asm
ld -o reverse reverse.o
clean:
rm reverse reverse.o
sum
sum.asm
;initialised data section
section .data
; Define constants
num1:equ 100
num2:equ 50
; http://geekswithblogs.net/MarkPearl/archive/2011/04/13/more-nasm.aspx
msg:db "Sum is correct", 10
;;
;; program code
;;
section .text
global_start
;; entry point
_start:
; get sum of num1 and num2
movrax, num1
movrbx, num2
addrax, rbx
; compare rax with correct sum - 150
cmprax, 150
; if rax is not 150 go to exit
jne.exit
; if rax is 150 print msg
jmp.rightSum
; Print message that sum is correct
.rightSum:
;; write syscall
movrax, 1
;; file descritor, standard output
movrdi, 1
;; message address
movrsi, msg
;; length of message
movrdx, 15
;; call write syscall
syscall
; exit from program
jmp.exit
; exit procedure
.exit:
movrax, 60
movrdi, 0
syscall
makefile
all:
nasm -f elf64 -o sum.o sum.asm
ld -o sum sum.o
clean:
rm sum sum.o
C语言与汇编之间调用
casm1 - call C function from asm
casm.c
#include
extern int print();
int print() {
printf("Hello World\n");
return 0;
}
casm.asm
global _start
extern print
section .text
_start:
callprint
movrax, 60
movrdi, 0
syscall
makefile
buildAsmc:
gcc -c casm.c -o c.o
nasm -f elf64 casm.asm -o casm.o
ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -lc casm.o c.o -o casm
clean:
rm -rf *.o
rm -rf casm
casm2 - gcc inline assembly
casm.c
#include
int main() {
char* str = "Hello World\n";
long len = strlen(str);
int ret = 0;
__asm__("movq $1, %%rax \n\t"
"movq $1, %%rdi \n\t"
"movq %1, %%rsi \n\t"
"movl %2, %%edx \n\t"
"syscall"
: "=g"(ret)
: "g"(str), "g" (len));
return 0;
}
makefile
build:
gcc casm.c -o casm
casm3 - call asm function from C
casm.c
#include
extern void printHelloWorld(char *str, int len);
int main() {
char* str = "Hello World\n";
int len = strlen(str);
printHelloWorld(str, len);
return 0;
}
casm.asm
global printHelloWorld
section .text
printHelloWorld:
;; 1 arg
movr10, rdi
;; 2 arg
movr11, rsi
;; call write syscall
movrax, 1
movrdi, 1
movrsi, r10
movrdx, r11
syscall
ret
makefile
build:
nasm -f elf64 -o casm.o casm.asm
gcc casm.o casm.c -o casm
标签:汇编,调用,syscall,mov,C语言,casm,rax,rdi,asm
来源: https://blog.csdn.net/Rong_Toa/article/details/88429259