Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
intarith
; intarith_64.asm show some simple C code and corresponding nasm code ; the nasm code is one sample, not unique ; ; compile: nasm -f elf64 -l intarith_64.lst intarith_64.asm ; link: gcc -m64 -nostartfiles -o intarith_64 intarith_64.o ; run: ./intarith_64 ; ; the output from running intarith_64.asm and intarith.c is: ; c=5 , a=3, b=4, c=5 ; c=a+b, a=3, b=4, c=7 ; c=a-b, a=3, b=4, c=-1 ; c=a*b, a=3, b=4, c=12 ; c=c/a, a=3, b=4, c=4 ; ;The file intarith.c is: ; /* intarith.c */ ; #include <stdio.h> ; int main() ; { ; long int a=3, b=4, c; ; c=5; ; printf("%s, a=%ld, b=%ld, c=%ld\n","c=5 ", a, b, c); ; c=a+b; ; printf("%s, a=%ld, b=%ld, c=%ld\n","c=a+b", a, b, c); ; c=a-b; ; printf("%s, a=%ld, b=%ld, c=%ld\n","c=a-b", a, b, c); ; c=a*b; ; printf("%s, a=%ld, b=%ld, c=%ld\n","c=a*b", a, b, c); ; c=c/a; ; printf("%s, a=%ld, b=%ld, c=%ld\n","c=c/a", a, b, c); ; return 0; ; } extern printf ; the C function to be called print_string: mov rdi, fmt4 ; first arg, format ;mov rsi, str ; second arg mov rdx, [a] ; third arg mov rcx, [b] ; fourth arg mov r8, [c] ; fifth arg mov rax, 0 ; no xmm used call printf ; Call C function ret section .data ; preset constants, writeable a: dq 3 ; 64-bit variable a initialized to 3 b: dq 4 ; 64-bit variable b initializes to 4 fmt4: db "%s, a=%ld, b=%ld, c=%ld",10,0 ; format string for printf addp: db "c=a+b",0 subp: db "c=a-b",0 multp: db "c=a*b",0 divp: db "c=c/a",0 section .bss ; unitialized space c: resq 1 ; reserve a 64-bit word str: resq 1 ; reserve a 94-bit word section .text ; instructions, code segment global _start ; for gcc standard linking _start: ; label lit5: ; c=5; mov rax,5 ; 5 is a literal constant mov [c],rax ; store into c addb: ; c=a+b; mov rax,[a] ; load a add rax,[b] ; add b mov [c],rax ; store into c mov rsi,addp call print_string subb: ; c=a-b; mov rax,[a] ; load a sub rax,[b] ; subtract b mov [c],rax ; store into c mov rsi,subp ; invoke the print call print_string mulb: ; c=a*b; mov rax,[a] ; load a (must be rax for multiply) imul qword [b] ; signed integer multiply by b mov [c],rax ; store bottom half of product into c mov rsi,multp ; invoke the print call print_string diva: ; c=c/a; mov rax,[c] ; load c mov rdx,0 ; load upper half of dividend with zero idiv qword [a] ; divide double register edx rax by a mov [c],rax ; store quotient into c mov rsi,divp ; invoke the print call print_string mov rax,60 ; exit code xor rdi,rdi syscall
run
|
edit
|
history
|
help
0
Input and Display a number
dsfdsdfafdsa
mortgage
dfdfdffddfdf
p1.asm
dfsfdsfdsfdsfasdf
dKleinhen Lab #1
dfsfsfsfsafd
dsadasfdsfds
afsdfds