Computer Labs: Assembly with the Netwide Assembler (NASM) 2O MIEIC

Computer Labs: Assembly with the Netwide Assembler (NASM) 2O MIEIC

Computer Labs: Assembly with the Netwide Assembler (NASM) 2o MIEIC Pedro F. Souto ([email protected]) September 28, 2010 NASM Command Line Options -h for usage instructions -o output file name -f output file format I Must be coff always -l generate listing file, i.e. file with code generated -e preprocess only -g enable debugging information Exemplo nasm -g -f coff foo.asm -o foo.o NASM Directives BITS 32 generate code for 32 bit processor mode CPU 386 | 686 | ... restrict assembly to the specified processor SECTION <section_name> specifies the section the assembly code will be assembled into. For COFF can be one of: .text code (program) section .data initialized data section .bss uninitialized data section EXTERN <symbol> declare <symbol> as declared elsewhere, allowing it to be used in the module; GLOBAL <symbol> declare <symbol> as global so that it can be used in other modules that import it via EXTERN Pseudo-Instructions for Data Definition ... in the .data section DB Define byte DW Define word (2 bytes) DD Define double word (4 bytes) IMPORTANT IA-32 architecture is little endian db 0x55 ; just byte 0x55 db 0x55, 0x56, 0x57 ; three bytes in succession db ’a’,0x55 ; character constants are OK db ’hello’,13,19,’$’ ; so are string constants dw 0x1234 ; == db 0x34, 0x12 dw ’a’ ; == db ’a’, 0x0 == db 0x41, 0x0 dw ’ab’ ; == db ’a’,’b’ dw ’abc’ ; == db ’a’,’b’,’c’, 0x0 dd 0x12345678 ; == db 0x78, 0x56, 0x34, 0x12 Pseudo-Instructions for Allocationg Space ... in the .bss section I It makes no sense to define data in the uninitialized section RESB Allocate byte RESW Allocate word (2 bytes) RESD Allocate double word (4 bytes) buffer: resb 64 ; allocate 64 bytes for buffer wordvar: resw 1 ; allocate word larray: resd 10 ; allocate array for 10 longs Other Pseudo-Instructions EQU Define symbolic constant message db ’hello, world’ msglen equ $-message Note The msglen is evaluated once using the value of $ at the point of definition $ evaluates to the assembly position at the beginning of the line containing the expression TIMES Prefix that causes the instruction to be assembled multiple times: zerobuf: times 64 db 0 buffer: db ’hello, world’ times buffer+64-$ db ’ ’ I What does buffer+64-$ evaluate to? NASM vs. MASM NASM is case sensitive I It makes a difference whether you call your label isr, Isr or ISR All memory references must use square brackets I.e. all effective addressses must appear between [ and ]. E.g.: mov eax,[ebx*2+ecx+offset] mov ax,[bp+di+8] NASM does not store variable types This means that you must specify the operand size. E.g.: var dw 0 mov word [var],2 ; and not: mov var, 2 C-Style Function Definition with NASM Macros int foo(int a, int b) { CPU 686 int tmp; BITS 32 tmp = a + b; %include ’macros.mac’ tmp = tmp - a * b; section .text return tmp; proc foo } %arg a: dword b:dword %local tmp: dword uses esi, edi mov esi, [a] mov eax, [b] add eax, esi mov [tmp], eax mov edi, [a] mov eax, [b] mul eax, edi sub [tmp], eax move eax, [tmp] ; return tmp endproc END Pixel() In C: void pixel(int x, int y, int color, char *base, int hres) { *(base + x + y*hres) = color; } In NASM: CPU 686 BITS 32 %include ’macros.mac’ section .text proc pixel %arg x: dword y:dword color:dword, base: dword, hres: dword uses mov ecx, [y] imul ecx, [hres] ; ecx = y*hres mov edx, [base] ; add edx, [x] ; edx = base +x mov al, [color] mov [ecx+dex], al endproc END Further Reading I Dr. Paul Carter, PC Assembly Language I Section 1.3: Assembly Language I Section 1.4: Creating a Program I NASM Manual I Section 2.2: Quick Start for MASM Users I Chapter 3: The NASM Language I Chapter 5: Assembler Directives I Section 8.1: Interfacing to 32-bit C Progarams I Appendix B: x86 Instruction Reference.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us