assembly language programming assembler and assembly
play

Assembly Language Programming Assembler and assembly language - PowerPoint PPT Presentation

Assembly Language Programming Assembler and assembly language Zbigniew Jurkiewicz, Instytut Informatyki UW October 24, 2017 Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language Assembler


  1. Assembly Language Programming Assembler and assembly language Zbigniew Jurkiewicz, Instytut Informatyki UW October 24, 2017 Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  2. Assembler Assembler = the program, which transform source file with program text in symbolic machine language to output file (called module) containing binary object code. A module contains the same program in machine langugage binary form. Assembler also produces other files containing program listing and cross-reference list. There are more than assembler for a given architecture (for example GNU Assembler), in labs we will use nasm assembler. Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  3. Assemblation To translate a program in file program.asm we should do (for 64 bit programs) bash-2.04$ nasm -f elf64 program.asm Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  4. Linking Program may contain more than one module, so before loading they should all be submitted to linkage processing. In the case of program contained fully in a single object module we call linker by bash-2.04$ ld program.o Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  5. Linking The linker usually creates an output file called a.out . To get the file with a different name, for example program , we should use option -o bash-2.04$ ld -o program program.o Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  6. Assembler The assembly language has many features, making writing machine langugage programs easier, but never breaks the basic rule: A single instruction of machine language corresponds to exactly single instruction of assembly language. Symbolic identifiers (labels) are used instead of numeric addresses. In addition to machine langugage instructions a program may contain additional lines with directives (also called pseudoinstructions or commands). They are performed at assembly time — when assembler transforms source program to machine language. In other words, they control the assembly process. Many assemblers offer additional mechanisms, like defining macrooperations and conditional assembly. Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  7. Syntax Assembly language instruction should be placed in a single line and has the following form: � label � : � operation � � arguments � ; � comment � for example start: add eax,ebx ;This is instruction Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  8. Syntax Only the operation field must always be filled, other fields are used when needed. Label is a symbolic representation of instruction address, to be used in control instructions, e.g. jmp start Labels in assembly code start at the first column and should be terminated with colon. Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  9. Syntax The directives have a slightly different form: � name � � directive � � arguments � ; � comment � Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  10. Constants Programs may contain the following kinds of constants: binary a sequence of binary digits terminated with the letter B , e.g. 10110011B . decimal hexadecimal should be preceded by 0x , e.g. 0xA5 . character a sequence of characters delimited by quotes, e.g. ’Ala’ . Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  11. Constants Symbolic constants are defined using the directive equ size equ 10 ... add ebx,size Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  12. Symbols Symbolic names are used to name variables, constants, labels, segments/sections, etc. With each symbol the assembler associates additional informations called attributes , like section , address and type . Predefined symbol $ is used for the assembly counter , its associated value is always equal to the offset (i.e. relative address) of the current line of program. It is cleared (set to zero) when starting to assemble a new segment. Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  13. Expressions Arguments of instructions and pseudoinstructions may contain expressions. The assembler replaces them in-line by their computed value, e.g. the following code uses expression to define the size of some data area info db 1,2,3,’Some string’,10 dl_info equ $-info Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  14. Memory reservation (“variables”) Memory declaration directives serve to declare and reserve data cells, and possibly to initialize their contents. Declaration to reserve one or more initialized bytes is � name � db � initial value � [,...] for example byte db 5 list db 1,2,3,4 mess db ’Message’,10 Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  15. Memory reservation (“variables”) If initialization is not needed we should use resb directive instead key resb 1 giving the number of reserved variables (in this case 1), so we can reserve a sequence cells, e.g. vector resb 25 Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  16. Memory reservation (“variables”) If we want to reserve some data area and fill it with repeating values, there is the prefix times , with the number of repetitions as argument numbers times 17 db 31 periodic times 30 db 1,2,3,4 (in the second case the consecutive cells will be initialized with values 1, 2, 3, 4, 1, 2, 3, 4, 1,...). Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  17. Memory reservation (“variables”) The directive dw reserves a word of memory (two bytes), and the directive dd reserves a double word (four bytes). Remember, that on Intel processors the line oper dw 0x4315 is equivalent to oper db 0x15,0x43 Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  18. Memory reservation (“variables”) Unfortunately NASM assembler does not store sizes associated with symbols (“types”), in its symbol table, so sometimes we have to prefix the address argument with size specification, e.g. mov word [oper],1 Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  19. Sections Program written in assembly language is divided into sections. Each section starts with section directive, e.g. to declare section with machine instructions (“code”) of a program we write section .text ... Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  20. Sections Some section names are standard: .text — contains executable instructions, .data — contains initialized data, and .bss — contains non-initialized data. Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  21. Sections Of course you may define your own sections with different names. Additional attributes are then used to specify the section’s purpose. Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  22. Exports Procedures called only locally in their module are equivalent to labels in code and need not be declared. Procedures intended to be called from other modules should be exported – declared as globals. The module (may be the only one) containing the main program should export the symbol _start . Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  23. Exports The exported symbols (e.g. procedure names) are declared using global � symbol � ,... Symbols are imported from other modules with the directive extern � symbol � ,... Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  24. Program structure Program in assembly language should have the following basic structure: ;; Definitions of constants ... ;; Data section section .data ... ;; Code section section .text global _start ;Main program;-) _start: ... ... mov eax,60 syscall Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

  25. Program structure ;; Procedures (in the same module) ... ;; Global non-initialized working data section .bss ... ;; End of program Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Assembler and assembly language

Recommend


More recommend