Docstoc

Condition Codes (PowerPoint)

Document Sample
Condition Codes (PowerPoint) Powered By Docstoc
					                            B.2.2 Condition Codes
       Condition code                               Explanation
                                                1. Jump above
     1. A / NBE                                 2. Jump if not below/equal
                                                used after a test of unsigned data;
                                                - if CF = 0 & ZF = 0, performs jump

                                               1. Jump if above/equal
     2. AE / NB                                2. Jump if not below
                                               used after a test of unsigned data;
                                               - if CF = 0, performs jump

                                                Jump if carry
     3. C                                       used after a test of unsigned data;
                                                - if CF = 1, performs jump

     4. JE/JZ                                   Jump if equal/zero
                                                used after a test of signed or unsigned data;
                                                - if ZF = 1, performs jump


Note: there are lot of conditional codes.
You can see all of them here or in the course book, pages 475-479.
Check your condition code in B.2.2 (Condition Codes) to be sure it is legal in NASM.
                       B.4.128 Jcc: Conditional Branch
B.4.128 Jcc: Conditional Branch
Jcc imm               (128-byte range)
Jcc NEAR imm          (full segment)
* for all the possible variants of operands look at NASM manual, B.4.128
Jump if and only if their conditions are satisfied.
Examples:
1. my_loop:
          sub ax, 2
          jz my_loop
In this case the condition to jump to my_loop label is the value of ZF.
If ZF is set (the result of sub ax, 2 is zero), jump would be performed.
If ZF is 0, the next instruction (that comes after jz my_loop) would be executed.
2. my_loop:
          cmp ax, 4
          ja my_loop
In this case the condition to jump to my_loop label is the value of ZF and CF.
If ax value is grater (above) than 4, jump would be performed.
                             Code ASCII
The standard ASCII code defines 128 character codes (from 0 to 127), of which,
the first 32 are control codes (non-printable), and the other 96 are representable
characters:




Example: the A character is located at the 4throw and the 1st column, for that it
would be represented in hexadecimal as 0x41.
Here you have an interactive Decimal-Hexadecimal-Octal-ASCII converter (at
the bottom of the page).
                                Running NASM

To assemble a file, you issue a command of the form
> nasm -f <format> <filename> [-o <output>]
Example:
> nasm -f elf mytry.s -o myelf.o
It would create myelf.o file that has elf format (executable and linkable format).
We use main.c file (that is written in C language) to start our program, and
sometimes also for input / output from a user. So to compile main.c with our
assembly file we should execute the following command:
> cc main.c myelf.o -o myexe.out
It would create executable file myexe.out.
In order to run it you should write its name on the command line:
> myexe.out
                                     main.c file

include <stdio.h>#
main(int argc, char ** argv) {
     int ac = atoi(argv[1]);               1. Include stdio.h library for doing I/O
    char str [10];
                                           2. You can get online parameters
    printf("Enter string: ");
    gets(str);                             3. Call to assembly function “my_func”
    printf("The string is: %s\n", str);
                                           4. Can contain another functions that we
                                           can call them from assembly file
    my_func(str);
    exit(0);
    return;
}
                          Assembly file
section .rodata                   1. Has two sections: .rodata & .text:
LC0: DB “Hello World %s”, 10, 0   .rodata contains all data declarations, .text
section .text                     contains a code
     align 16
     global my_func               2. Align 16 means that all data and
     extern printf                instruction should be at an address that is
                                  divided by 16 (bits) (in another words, an
my_func:                          address should be even)
   push ebp                       3. my_func is a function that we define; it
   mov ebp, esp                   should be global so that main.c file can call
   sub esp, 8                     to it

   push dword [ebp + 8]           4. We use printf function of stdlib.h C
   push LCO                       library; so we should declare it as an
   call printf                    external function because it isn’t defined in
                                  our assembly file (if we use any function that is
                                  not in our file, we should declare it as external)
   add dword esp, 16
   mov esp, ebp                   5. Purpose of my_func is to call to printf to
   pop ebp                        print a string that it got (from main)
   ret
                                Task 0
You should use asm.s and main.c file for this task.




                                    Here should be your code
                                    Task 1
In this task we would use asm.s assembly file and main.c C file.




                              Here should be your code
                        Labels definition (advanced)




- valid characters in labels are: letters, numbers, _, $, #, @, ~, ., and ?
- first character can be: letter, _, ? and . ( . has a special meaning)
- label can be prefixed with a $ to indicate that it is intended to be read as an
identifier and not a reserved word
Example:
if some other module you are linking with defines a symbol called eax, you can refer to
$eax in NASM code to distinguish the symbol from the register.
     3.2.1 DB, DW, DD, DQ and DT: declaring initialized data

DB – define byte; DW – define word etc.

Examples:
1. my_var db 0x55                 ; just the byte 0x55
2. my_var db 0x55,0x56,0x57       ; three bytes in succession
3. my_var db 'a',0x55             ; character constants are OK
4. my_var db 'hello',13,10,'$'    ; so are string constants
5. my_var dw 0x1234               ; 0x34 0x12
6. my_var dw 'a'                  ; 0x41 0x00 (it's just a number)
7. my_var dw 'ab'                 ; 0x41 0x42 (character constant)
Notes:
- DD can be used for numeric constants and floating-point numbers as well
- DQ and DT do not accept numeric constants or string constants as
operands, only floating-point numbers
           3.2.2. RESB, RESW, RESD, RESQ AND REST:
                  declaring uninitialized storage space


Example:
1. buffer: resb 64           ; reserve 64 bytes
2. word_var: resw 1          ; reserve a word
3. real_array resq 10        ; array of ten real numbers


Note: you can not make any assumption about values of a storage space cells.
3.2.4 EQU: defining constants

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:16
posted:3/3/2012
language:English
pages:12