日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

nasm简单实例

發布時間:2025/7/14 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nasm简单实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

Sample nasm programs

Specifically: for Intel I-32, e.g. 386, 486, pentium

Specifically: for use with gcc with its libraries and gdb

Specifically: simple nasm syntax using "C" literals

Specifically: showing an equivalent "C" program

Generally, for Linux and possibly other Unix on Intel

Generally, not using 8-bit or 16-bit for anything

Contents

Makefile for samples shown below hello.asm basic stand alone program printf1.asm basic calling printf printf2.asm more types for printf intarith.asm simple integer arithmetic fltarith.asm simple floating point arithmetic

hello.asm basic stand alone program

The nasm source code is hello.asm The result of the assembly is hello.lst Running the program produces output hello.out This program demonstrates basic text output to a screen.No "C" library functions are used.Calls are made to the operating system directly. (int 80 hex); hello.asm a first program for nasm for Linux, Intel, gcc ; ; assemble: nasm -f elf -l hello.lst hello.asm ; link: gcc -o hello hello.o ; run: hello ; output is: Hello World SECTION .data ; data section msg: db "Hello World",10 ; the string to print, 10=cr len: equ $-msg ; "$" means "here"; len is a value, not an addressSECTION .text ; code sectionglobal main ; make label available to linker main: ; standard gcc entry pointmov edx,len ; arg3, length of string to printmov ecx,msg ; arg2, pointer to stringmov ebx,1 ; arg1, where to write, screenmov eax,4 ; write sysout command to int 80 hexint 0x80 ; interrupt 80 hex, call kernelmov ebx,0 ; exit code, 0=normalmov eax,1 ; exit command to kernelint 0x80 ; interrupt 80 hex, call kernel

printf1.asm basic calling printf

The nasm source code is printf1.asm The result of the assembly is printf1.lst The equivalent "C" program is printf1.c Running the program produces output printf1.out This program demonstrates basic use of "C" library function printf.The equivalent "C" code is shown as comments in the assembly language.; printf1.asm print an integer from storage and from a register ; Assemble: nasm -f elf -l printf.lst printf1.asm ; Link: gcc -o printf1 printf1.o ; Run: printf1 ; Output: a=5, eax=7; Equivalent C code ; /* printf1.c print an int and an expression */ ; #include ; int main() ; { ; int a=5; ; printf("a=%d, eax=%d\n", a, a+2); ; return 0; ; }; Declare some external functions ;extern printf ; the C function, to be calledSECTION .data ; Data section, initialized variablesa: dd 5 ; int a=5; fmt: db "a=%d, eax=%d", 10, 0 ; The printf format, "\n",'0'SECTION .text ; Code section.global main ; the standard gcc entry point main: ; the program label for the entry pointpush ebp ; set up stack framemov ebp,espmov eax, [a] ; put a from store into registeradd eax, 2 ; a+2push eax ; value of a+2push dword [a] ; value of variable apush dword fmt ; address of ctrl stringcall printf ; Call C functionadd esp, 12 ; pop stack 3 push times 4 bytesmov esp, ebp ; takedown stack framepop ebp ; same as "leave" opmov eax,0 ; normal, no error, return valueret ; return

printf2.asm more types with printf

The nasm source code is printf2.asm The result of the assembly is printf2.lst The equivalent "C" program is printf2.c Running the program produces output printf2.out This program demonstrates basic use of "C" library function printf.The equivalent "C" code is shown as comments in the assembly language.; printf2.asm use "C" printf on char, string, int, double ; ; Assemble: nasm -f elf -l printf2.lst printf2.asm ; Link: gcc -o printf2 printf2.o ; Run: printf2 ; Output: ;Hello world: a string of length 7 1234567 6789ABCD 5.327000e-30 -1.234568E+302 ; ; A similar "C" program ; #include ; int main() ; { ; char char1='a'; /* sample character */ ; char str1[]="string"; /* sample string */ ; int int1=1234567; /* sample integer */ ; int hex1=0x6789ABCD; /* sample hexadecimal */ ; float flt1=5.327e-30; /* sample float */ ; double flt2=-123.4e300; /* sample double */ ; ; printf("Hello world: %c %s %d %X %e %E \n", /* format string for printf */ ; char1, str1, int1, hex1, flt1, flt2); ; return 0; ; }extern printf ; the C function to be calledSECTION .data ; Data sectionmsg: db "Hello world: %c %s of length %d %d %X %e %E",10,0; format string for printf char1: db 'a' ; a character str1: db "string",0 ; a C string, "string" needs 0 len: equ $-str1 ; len has value, not an address inta1: dd 1234567 ; integer 1234567 hex1: dd 0x6789ABCD ; hex constant flt1: dd 5.327e-30 ; 32-bit floating point flt2: dq -123.456789e300 ; 64-bit floating pointSECTION .bssflttmp: resq 1 ; 64-bit temporary for printing flt1SECTION .text ; Code section.global main ; "C" main program main: ; label, start of main programfld dword [flt1] ; need to convert 32-bit to 64-bitfstp qword [flttmp] ; floating load makes 80-bit,; store as 64-bit; push last argument firstpush dword [flt2+4] ; 64 bit floating point (bottom)push dword [flt2] ; 64 bit floating point (top)push dword [flttmp+4] ; 64 bit floating point (bottom)push dword [flttmp] ; 64 bit floating point (top)push dword [hex1] ; hex constantpush dword [inta1] ; integer data pass by valuepush dword len ; constant pass by valuepush dword str1 ; "string" pass by reference push dword [char1] ; 'a'push dword msg ; address of format stringcall printf ; Call C functionadd esp, 40 ; pop stack 10*4 bytesmov eax, 0 ; exit code, 0=normalret ; main returns to operating system

intarith.asm simple integer arithmetic

The nasm source code is intarith.asm The result of the assembly is intarith.lst The equivalent "C" program is intarith.c Running the program produces output intarith.out This program demonstrates basic integer arithmetic add, subtract,multiply and divide.The equivalent "C" code is shown as comments in the assembly language.; intarith.asm show some simple C code and corresponding nasm code ; the nasm code is one sample, not unique ; ; compile: nasm -f elf -l intarith.lst intarith.asm ; link: gcc -o intarith intarith.o ; run: intarith ; ; the output from running intarith.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 ; int main() ; { ; int a=3, b=4, c; ; ; c=5; ; printf("%s, a=%d, b=%d, c=%d\n","c=5 ", a, b, c); ; c=a+b; ; printf("%s, a=%d, b=%d, c=%d\n","c=a+b", a, b, c); ; c=a-b; ; printf("%s, a=%d, b=%d, c=%d\n","c=a-b", a, b, c); ; c=a*b; ; printf("%s, a=%d, b=%d, c=%d\n","c=a*b", a, b, c); ; c=c/a; ; printf("%s, a=%d, b=%d, c=%d\n","c=c/a", a, b, c); ; return 0; ; }extern printf ; the C function to be called%macro pabc 1 ; a "simple" print macrosection .data .str db %1,0 ; %1 is first actual in macro callsection .text; push onto stack backwards push dword [c] ; int cpush dword [b] ; int b push dword [a] ; int apush dword .str ; users stringpush dword fmt ; address of format stringcall printf ; Call C functionadd esp,20 ; pop stack 5*4 bytes %endmacrosection .data ; preset constants, writeable a: dd 3 ; 32-bit variable a initialized to 3 b: dd 4 ; 32-bit variable b initializes to 4 fmt: db "%s, a=%d, b=%d, c=%d",10,0 ; format string for printfsection .bss ; unitialized space c: resd 1 ; reserve a 32-bit wordsection .text ; instructions, code segmentglobal main ; for gcc standard linking main: ; labellit5: ; c=5;mov eax,5 ; 5 is a literal constantmov [c],eax ; store into cpabc "c=5 " ; invoke the print macroaddb: ; c=a+b;mov eax,[a] ; load aadd eax,[b] ; add bmov [c],eax ; store into cpabc "c=a+b" ; invoke the print macrosubb: ; c=a-b;mov eax,[a] ; load asub eax,[b] ; subtract bmov [c],eax ; store into cpabc "c=a-b" ; invoke the print macromulb: ; c=a*b;mov eax,[a] ; load a (must be eax for multiply)imul dword [b] ; signed integer multiply by bmov [c],eax ; store bottom half of product into cpabc "c=a*b" ; invoke the print macrodiva: ; c=c/a;mov eax,[c] ; load cmov edx,0 ; load upper half of dividend with zeroidiv dword [a] ; divide double register edx eax by amov [c],eax ; store quotient into cpabc "c=c/a" ; invoke the print macromov eax,0 ; exit code, 0=normalret ; main return to operating system

fltarith.asm simple floating point arithmetic

The nasm source code is fltarith.asm The result of the assembly is fltarith.lst The equivalent "C" program is fltarith.c Running the program produces output fltarith.out This program demonstrates basic floating point add, subtract,multiply and divide.The equivalent "C" code is shown as comments in the assembly language.; fltarith.asm show some simple C code and corresponding nasm code ; the nasm code is one sample, not unique ; ; compile nasm -f elf -l fltarith.lst fltarith.asm ; link gcc -o fltarith fltarith.o ; run fltarith ; ; the output from running fltarith and fltarithc is: ; c=5.0, a=3.000000e+00, b=4.000000e+00, c=5.000000e+00 ; c=a+b, a=3.000000e+00, b=4.000000e+00, c=7.000000e+00 ; c=a-b, a=3.000000e+00, b=4.000000e+00, c=-1.000000e+00 ; c=a*b, a=3.000000e+00, b=4.000000e+00, c=1.200000e+01 ; c=c/a, a=3.000000e+00, b=4.000000e+00, c=4.000000e+00 ; ;The file fltarith.c is: ; #include ; int main() ; { ; double a=3.0, b=4.0, c; ; ; c=5.0; ; printf("%s, a=%e, b=%e, c=%e\n","c=5.0", a, b, c); ; c=a+b; ; printf("%s, a=%e, b=%e, c=%e\n","c=a+b", a, b, c); ; c=a-b; ; printf("%s, a=%e, b=%e, c=%e\n","c=a-b", a, b, c); ; c=a*b; ; printf("%s, a=%e, b=%e, c=%e\n","c=a*b", a, b, c); ; c=c/a; ; printf("%s, a=%e, b=%e, c=%e\n","c=c/a", a, b, c); ; return 0; ; }extern printf ; the C function to be called%macro pabc 1 ; a "simple" print macrosection .data .str db %1,0 ; %1 is macro call first actual parametersection .text; push onto stack backwards push dword [c+4] ; double c (bottom)push dword [c] ; double cpush dword [b+4] ; double b (bottom)push dword [b] ; double b push dword [a+4] ; double a (bottom)push dword [a] ; double apush dword .str ; users stringpush dword fmt ; address of format stringcall printf ; Call C functionadd esp,32 ; pop stack 8*4 bytes %endmacrosection .data ; preset constants, writeable a: dq 3.333333333 ; 64-bit variable a initialized to 3.0 b: dq 4.444444444 ; 64-bit variable b initializes to 4.0 five: dq 5.0 ; constant 5.0 fmt: db "%s, a=%e, b=%e, c=%e",10,0 ; format string for printfsection .bss ; unitialized space c: resq 1 ; reserve a 64-bit wordsection .text ; instructions, code segmentglobal main ; for gcc standard linking main: ; labellit5: ; c=5.0;fld qword [five] ; 5.0 constantfstp qword [c] ; store into cpabc "c=5.0" ; invoke the print macroaddb: ; c=a+b;fld qword [a] ; load a (pushed on flt pt stack, st0)fadd qword [b] ; floating add b (to st0)fstp qword [c] ; store into c (pop flt pt stack)pabc "c=a+b" ; invoke the print macrosubb: ; c=a-b;fld qword [a] ; load a (pushed on flt pt stack, st0)fsub qword [b] ; floating subtract b (to st0)fstp qword [c] ; store into c (pop flt pt stack)pabc "c=a-b" ; invoke the print macromulb: ; c=a*b;fld qword [a] ; load a (pushed on flt pt stack, st0)fmul qword [b] ; floating multiply by b (to st0)fstp qword [c] ; store product into c (pop flt pt stack)pabc "c=a*b" ; invoke the print macrodiva: ; c=c/a;fld qword [c] ; load c (pushed on flt pt stack, st0)fdiv qword [a] ; floating divide by a (to st0)fstp qword [c] ; store quotient into c (pop flt pt stack)pabc "c=c/a" ; invoke the print macromov eax,0 ; exit code, 0=normalret ; main returns to operating system

Go to top

Last updated 10/10/03

轉載于:https://my.oschina.net/wxwHome/blog/55861

總結

以上是生活随笔為你收集整理的nasm简单实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。