Nasmtutorial

Yep, it’s an educational.Scope of the Tutorial

This educational will display you the way to write assembly language programs at the x86-64 structure.

You will write each (1) standalone programs and (2) applications that integrate with C.

We gained’t get too fancy.Your First Program

Before learning about nasm, allow’s make certain you could kind in and run packages.

Make sure each nasm and gcc are installed. Save one of the following programs as hiya.asm, depending in your machine platform. Then run this system in line with the given instructions.

If you are on a Linux-based totally OS:

good day.asm; ----------------------------------------------------------------------------------------; Writes "Hello, World" to the console the usage of only system calls. Runs on sixty four-bit Linux most effective.; To assemble and run:;;nasm -felf64 hiya.asm && ld hello.o && ./a.out; ----------------------------------------------------------------------------------------global_startsection.text_start:movrax, 1; machine name for writemovrdi, 1; file deal with 1 is stdoutmovrsi, message; deal with of string to outputmovrdx, thirteen; range of bytessyscall; invoke operating machine to do the writemovrax, 60; gadget call for exitxorrdi, rdi; go out code 0syscall; invoke running gadget to exitsection.datamessage:db"Hello, World", 10; observe the newline on the end$ nasm -felf64 hey.asm && ld good day.o && ./a.outHello, World

howdy.asm; ----------------------------------------------------------------------------------------; Writes "Hello, World" to the console the use of best device calls. Runs on 64-bit macOS most effective.; To bring together and run:;;nasm -fmacho64 hello.asm && ld howdy.o && ./a.out; ----------------------------------------------------------------------------------------globalstartsection.textstart:movrax, 0x02000004; machine name for writemovrdi, 1; file take care of 1 is stdoutmovrsi, message; address of string to outputmovrdx, 13; variety of bytessyscall; invoke running gadget to do the writemovrax, 0x02000001; device name for exitxorrdi, rdi; go out code 0syscall; invoke running device to exitsection.datamessage:db"Hello, World", 10; word the newline at the give up$ nasm -fmacho64 howdy.asm && ld hello.o && ./a.outHello, World

Exercise: Identify the variations among the two packages.Structure of a NASM Program

NASM is line-based. Most packages consist of directives observed by way of one or greater sections. Lines can have an elective label. Most traces have an guidance observed through 0 or extra operands.

Generally, you placed code in a section called .textual content and your regular facts in a section called .statistics.Details

NASM is an splendid assembler, however meeting language is complex. You want more than an instructional. You want information. Lots of info. Be geared up to consult:The NASM Manual, which is pretty true!The Intel Processor ManualsYour First Few Instructions

There are hundreds of instructions. You can’t study them . Just begin with those:mov x, y      x ← yand x, yx ← x and yor x, yx ← x or yxor x, yx ← x xor yupload x, yx ← x + ysub x, yx ← x – yinc xx ← x + 1dec xx ← x – 1syscallInvoke an operating gadget routinedbA pseudo-instructionthat announces bytes on the way to be in memory while the program runsThe Three Kinds of OperandsRegister Operands

In this educational we simplest care about the integer registers and the xmmregisters. You ought to already recognize what the registers are, however here is a quickreview. The 16 integer registers are sixty four bits wide and are known as:R0R1R2R3R4R5R6R7R8R9R10R11R12R13R14R15RAX RCX RDX RBX RSP RBP RSI RDI

(Note that 8 of the registers have trade names.) You can deal with the lowest32-bits of every register as a check in itself but the use of these names:R0D R1D R2D R3D R4D R5D R6D R7D R8D R9D R10D R11D R12D R13D R14D R15DEAX ECX EDX EBX ESP EBP ESI EDI

You can treat the bottom sixteen-bits of every register as a register itself however using these names:R0W R1W R2W R3W R4W R5W R6W R7W R8W R9W R10W R11W R12W R13W R14W R15WAXCXDXBXSPBPSIDI

You can deal with the lowest eight-bits of each sign up as a register itself however using those names:R0B R1B R2B R3B R4B R5B R6B R7B R8B R9B R10B R11B R12B R13B R14B R15BALCLDLBLSPL BPL SIL DIL

For ancient motives, bits 15 via 8 of R0..R3 are named:AHCHDHBH

And subsequently, there are 16 XMM registers, each 128 bits extensive, named:XMM0 ... XMM15

Study this photo; hopefully it helps:

These are the fundamental kinds of addressing:[ variety ][ reg ][ reg + reg*scale ]      scale is 1, 2, 4, or 8 most effective[ reg + wide variety ][ reg + reg*scale + wide variety ]

The number is called the displacement; the obvious check in is calledthe base; the register with the dimensions is called the index.

Examples:[750]; displacement best[rbp]; base sign up only[rcx + rsi*4]; base + index * scale[rbp + rdx]; scale is 1[rbx - 8]; displacement is -eight[rax + rdi*8 + 500]; all four components[rbx + counter]; makes use of the cope with of the variable 'counter' as the displacementImmediate Operands

These may be written in many ways. Here are a few examples from the reputable doctors.two hundred; decimal0200; still decimal - the main 0 does not make it octal0200d; explicitly decimal - d suffix0d200; additionally decimal - 0d prefex0c8h; hex - h suffix, but leading 0 is required because c8h looks like a var0xc8; hex - the conventional 0x prefix0hc8; hex - for a few motive NASM likes 0h310q; octal - q suffix0q310; octal - 0q prefix11001000b; binary - b suffix0b1100_1000; binary - 0b prefix, and via the manner, underscores are allowedInstructions withreminiscence operands are extremely uncommon

In fact, we’ll not see this type of education on this academic. Most of the basic commands have onlythe following bureaucracy:add reg, regadd reg, memupload reg, immadd mem, regupload mem, immDefining Data and Reserving Space

These examples come from Chapter three of the doctors. To vicinity information in memory:db0x55; simply the byte 0x55db0x55,0x56,0x57; three bytes in successiondb'a',0x55; person constants are OKdb'hey',thirteen,10,'$'; so are string constantsdw0x1234; 0x34 0x12dw'a'; 0x61 0x00 (it is simply a variety of)dw'ab'; 0x61 0x62 (man or woman steady)dw'abc'; 0x61 0x62 0x63 0x00 (string)dd0x12345678; 0x78 0x56 0x34 0x12dd1.234567e20; floating-point constantdq0x123456789abcdef0; 8 byte constantdq1.234567e20; double-precision floatdt1.234567e20; extended-precision float

There are different kinds; test the NASM medical doctors. Later.

To reserve space (without initializing), you can use the subsequent pseudo instructions. Theyshould move in a phase called .bss (you will get an blunders if you attempt to use them ina .textual content phase):buffer:resb64; reserve sixty four byteswordvar:resw1; reserve a wordrealarray:resq10; array of ten realsAnother Example

Here’s a macOS software to look at:

triangle.asm; ----------------------------------------------------------------------------------------; This is an OSX console application that writes a little triangle of asterisks to traditional; output. Runs on macOS best.;;nasm -fmacho64 triangle.asm && gcc hola.o && ./a.out; ----------------------------------------------------------------------------------------globalstartsection.textstart:movrdx, output; rdx holds cope with of next byte to writemovr8, 1; preliminary line lengthmovr9, zero; number of stars written on line so farline:movbyte [rdx], '*'; write unmarried starincrdx; improve pointer to subsequent cellular to writeincr9; "depend" number so far on linecmpr9, r8; did we attain the number of stars for this line?jneline; no longer yet, keep writing on this linelineDone:movbyte [rdx], 10; write a new line charincrdx; and circulate pointer to in which next char goesincr8; next line might be one char longermovr9, 0; reset remember of stars written on this linecmpr8, maxlines; wait, did we already finish the final line?jngline; if not, start penning this linedone:movrax, 0x02000004; machine name for writemovrdi, 1; report deal with 1 is stdoutmovrsi, output; cope with of string to outputmovrdx, dataSize; number of bytessyscall; invoke operating machine to do the writemovrax, 0x02000001; system call for exitxorrdi, rdi; exit code 0syscall; invoke running machine to exitsection.bssmaxlinesequ8dataSizeequ44output:resbdataSize$ nasm -fmacho64 triangle.asm && ld triangle.o && ./a.out************************************

Post a Comment for "Nasmtutorial"