programming in assembly language
play

Programming in Assembly Language Minimal Program Move CS - PowerPoint PPT Presentation

Programming in Assembly Language Minimal Program Move CS Basics Flags Increment and decrement 5) Programming in Conditional jumps Assembly Language Signed Values Structure of a program Emmanuel Benoist The Stack


  1. Programming in Assembly Language Minimal Program � Move � CS Basics Flags � Increment and decrement 5) Programming in Conditional jumps Assembly Language Signed Values � Structure of a program � Emmanuel Benoist The Stack � Fall Term 2016-17 Interrupts � Software Interrupts Hardware Interrupt System call 64-bit � Conclusion � Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 1 2 Minimal Program For studying functionalities we need a “sandbox” Minimal Program To play with That do not do anything important That we can totaly destruct and rebuild Solution A program with no instruction, where we will try new features Only one thing is obligatory: a start point Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 3 4

  2. Assembly Language Sandbox sandbox.asm section .data section .text global _start Move _start: nop ; Put your experiments between the two ց → nops... nop ; Put your experiments between the two ց → nops... Attention: This program does not terminate properly, it is just for testing purpose inside a debugger, Terminate the program manualy otherwise it will generate a core dump Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 5 6 The MOV instruction Immediate Data Immediate addressing The data is built right into the machin instruction itself Moves data from one location to another It is neither in a register, nor in a data item Syntax Intruction is in the instruction mov destination, source mov RAX, 42 Is the most used command mov RBX, ’Hello’ Move information from register to memory mov RCX, 0ABCDh from memory to register from register to register Hello is stored in reverse order But NOT form memory to memory Because of “little endian” Examples Size must be compatible mov RAX, 42 cl is a 8-bit register 067EFh is 16 bits mov cl, 067EFh ; Instruction is not accepted by ց → NASM Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 7 8

  3. Move Register Data Example Register Addressing Test the following in a debugger Name the register to work with mov ax,067FEh mov EBP, ESI ; 32-bit mov bx, ax mov BL, CH ; 8-bit mov cl,bh add DX, AX ; 16-bit mov ch,bl add ECX, EDX ; 32-bit Set the breakpoint on the first instruction add RAX, RBX ; 64-bit Single-step through the instructions ADD instruction watch carefully Adds the source and the destination Halve registers (8-bit) are used for accessing RCX The sum replaces whatever is in the destination Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 9 10 Memory Data Memory Data (Cont.) Is stored in the memory “owned” by the program Can be access using general purpose registers ;; Move to and from memory mov rbx, Snippet ;the address of the string ց Size copied → in memory The size of data copied from the memory depends on the size mov rcx, 3 ; the offset in the string of the register mov ax, [rbx] ; Move 16 bits ax is 16-bit mov rax, [rbx+3] ; Move 64 bits eax is 32-bit mov eax, [rbx+rcx] ; Move 32 bits rax is 64-bit The [] operator Accesses the memory at the address inside the brackets [ebx] referes to memory whose address is stored in ebx [ebx+3] the memory whose address is the value of ebx plus 3 [ebx+ecx] the memory whose address is the sum of the values of ebx and ecx Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 11 12

  4. Confusing Data and its Address Label are addresses EatMsg is the address of the string [EatMsg] is the content of the address For a 32-bit register, we transfer 4 bytes For a 64-bit register, we transfer 8 bytes Flags section .data EatMsg db "Eat�at�Joe’s" section .text global _start _start: nop ; Put your experiments between the two ց → nops mov rcx, EatMsg ; copy the address mov rdx, [EatMsg] ; Copy 64 first bits of the ց → message nop ; Put your experiments between the two ց → nops Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 13 14 Flags Flags (Cont.) The EFLAG register contains 32 bits: each one could be a flag IF: Interrupt enable flag Contains 18 Flags The CPU can set it We present the most usefull ones You can set it using STI (set IF) and CLI (clear IF) Most of the flags represent a “result” of some kind When IF is set, interrupts are enabled and may occur when OF: Overflow Flag requested When IF is cleared interrupts are ignored When an arithmetic operation on a signed integer quantity becomes too large TF: Trap Flag Is generally used as a “carry” flag in signed arithmetic allows debuggers to manage single-stepping DF: Direction Flag it forces the CPU to execute only a single instruction It tells the CPU something you want SF: Sign Flag Tells the direction you want for string instruction becomes set, when the result of an operation forces the If DF = 1 string instructions proceed from high memory operand to become negative toward low memory means: the first bit becomes 1 during the operation if DF=0 string instructions proceed from low memory toward high memory. Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 15 16

  5. Flags (Cont.) ZF: Zero Flag is set when the result of an operation becomes zero. Increment and decrement PF: Parity Flag Familiar with serial data communication Indicates if the number of ones in the low-order byte is even or odd CF: Carry Flag is used in unsigned arithmetic operations if the result “carries out” a bit from the operand Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 17 18 Increment and decrement INC: Increment Adds one to its operand DEC: decrement Substract one from its operand Conditional jumps Example mov eax, 0FFFFFFFFh mov ebx, 02Dh dec ebx inc eax ebx becomes 2Ch eax becomes 0 Carry Flag is not affected by INC The last instruction changes Flags PF (parity), AF (Auxillary), ZF (zero), IF (interrupt) are set Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 19 20

  6. Conditional jumps Kangaroo Kangaroo.asm section .data Most of the flags have a conditional jump Snippet db "KANGAROO" JNZ : Jump if not zero section .text If ZF is clear, nothing is done global _start If ZF is set, execution travels to a new destination _start: Example: First loop nop ; Put your experiments between the two nops... ;; A first loop mov ebx,Snippet mov eax, 5 mov eax,8 DoMore: dec eax DoMore: add byte [ebx],32 jnz DoMore inc ebx dec eax As long as eax is not zero, loops to DoMore jnz DoMore ; Put your experiments between the two nops... nop Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 21 22 Kangaroo (Cont.) The string “KANGAROO” is stored in memory Signed Values It receives a label Snippet eax is a counter that is decremented ebx is incremented from the Snippet label, to the end of the string Letters are changed to lowercase One substracts 32 to all the letters We can check in the memory Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 23 24

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend