Download presentation
Presentation is loading. Please wait.
Published byDaniella Copeland Modified over 9 years ago
1
1 Assembly Language Part 2 Professor Jennifer Rexford COS 217
2
2 Goals of Today’s Lecture Machine language Encoding the operation and the operands Simpler MIPS instruction set as an example More on IA32 assembly language Different sizes of data Example instructions Addressing modes Layout of assembly language program
3
3 Machine Language Using MIPS Architecture as an Example (since it has a simpler instruction set than IA32)
4
4 Three Levels of Languages High-level languages (e.g., Java and C) Easier programming by describing operations in a natural language Increased portability of the code Assembly language (e.g., IA32 and MIPS) Tied to the specifics of the underlying machine Instructions and names to make code human readable Machine language Also tied to the specifics of the underlying machine In binary format the computer can read and execute Every instruction is a sequence of one or more numbers
5
Machine-Language Instructions An ADD Instruction: add r1 = r2 + r3 (assembly) Parts of the Instruction: Opcode (verb) – what operation to perform Operands (noun) – what to operate upon Source Operands – where values come from Destination Operand – where to deposit data values Opcode Operands
6
6 Machine-Language Instruction Opcode What to do Source operand(s) Immediate (in the instruction itself) Register Memory location I/O port Destination operand Register Memory location I/O port Assembly syntax Opcode source1, [source2,] destination
7
7 MIPS Has Three Kinds of 32-bit Instructions R: Registers Two source registers (rs and rt) One destination register (rd) E.g., “rd = rs + rt” or “rd = rs & rt” or “rd = rs xor rt” oprsrdrt shamtfunct Operation and specific variant Shift amount
8
8 MIPS Has Three Kinds of 32-bit Instructions I: Immediate, transfer, branch One source register (rs) and one 16-bit constant (imm) One destination register (rd) E.g., “rd = rs + imm” or “rd = rs & imm” E.g., “rd = MEM[rs + imm]” (treating rs+imm as address) E.g., “jump to address contained in rs” (rs as address) E.g., “jump to word imm if rs is 0” (i.e., change instruction pointer) oprsrdaddress/immediate
9
9 MIPS Has Three Kinds of 32-bit Instructions J: Jump One 28-bit constant (imm) for # of 32-bit words to jump E.g., “jump by imm words” (i.e., change the instruction pointer) optarget address
10
10 MIPS “Add” Instruction Encoding Add registers 18 and 19, and store result in register 17. 0 18 19 17 0 32 add is an R inst
11
11 MIPS “Subtract” Instruction Encoding Subtract register 19 from register 18 and store in register 17 0 18 19 17 0 34 sub is an R inst
12
12 Greater Detail on IA32 Assembly: Instruction Set and Data Sizes
13
13 movl%edx, %eax andl$1, %eax je.else jmp.endif.else:.endif: sarl$1, %edx movl%edx, %eax addl%eax, %edx addl$1, %edx addl$1, %ecx.loop: cmpl$1, %edx jle.endloop jmp.loop.endloop: movl$0, %ecx Earlier Example count=0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } n %edx count %ecx
14
14 Size of Variables Data types in high-level languages vary in size Character: 1 byte Short, int, and long: varies, depending on the computer Pointers: typically 4 bytes Struct: arbitrary size, depending on the elements Implications Need to be able to store and manipulate in multiple sizes Byte (1 byte), word (2 bytes), and extended (4 bytes) Separate assembly-language instructions –e.g., addb, addw, addl Separate ways to access (parts of) a 4-byte register
15
15 Four-Byte Memory Words Memory 2 32 -1 0 Byte order is little endian 3108 716 15...... 24 23 Byte 4 Byte 0 Byte 5 Byte 1Byte 2 Byte 6 Byte 3 Byte 7
16
16 IA32 General Purpose Registers General-purpose registers EAX EBX ECX EDX ESI EDI 31 0 AX BX CX DX 16-bit 32-bit DI SI AL AH BL CL DL BH CH DH 8 715
17
17 Arithmetic Instructions Simple instructions add{b,w,l} source, destdest = source + dest sub{b,w,l} source, destdest = dest – source Inc{b,w,l} destdest = dest + 1 dec{b,w,l} destdest = dest – 1 neg{b,w,l} destdest = ^dest cmp{b,w,l} source1, source2source2 – source1 Multiply mul (unsigned) or imul (signed) mull %ebx# edx, eax = eax * ebx Divide div (unsigned) or idiv (signed) idiv %ebx# edx = edx,eax / ebx Many more in Intel manual (volume 2) adc, sbb, decimal arithmetic instructions
18
18 Bitwise Logic Instructions Simple instructions and{b,w,l} source, destdest = source & dest or{b,w,l} source, destdest = source | dest xor{b,w,l} source, destdest = source ^ dest not{b,w,l} destdest = ^dest sal{b,w,l} source, dest (arithmetic)dest = dest << source sar{b,w,l} source, dest (arithmetic)dest = dest >> source Many more in Intel Manual (volume 2) Logic shift Rotation shift Bit scan Bit test Byte set on conditions
19
19 Branch Instructions Conditional jump j{l,g,e,ne,...} targetif (condition) {eip = target} Unconditional jump jmp target jmp *register ComparisonSignedUnsigned ee “equal” ne “not equal” >ga “greater,above” geae “...-or-equal” <lb “less,below” lebe “...-or-equal” overflow/carry oc no ovf/carry nonc
20
20 Setting the EFLAGS Register Comparison cmpl compares two integers Done by subtracting the first number from the second –Discarding the results, but setting the eflags register Example: –cmpl $1, %edx (computes %edx – 1) –jle.endloop (looks at the sign flag and the zero flag) Logical operation andl compares two integers Example: –andl $1, %eax (bit-wise AND of %eax with 1) –je.else (looks at the zero flag) Unconditional branch jmp Example: –jmp.endif and jmp.loop
21
21 EFLAG Register & Condition Codes CFCF 1 PFPF 0 AFAF 0 ZFZF SFSF TFTF IFIF DFDF OFOF IO P L NTNT 0 RFRF VMVM ACAC VIFVIF VIPVIP IDID Reserved (set to 0) 012345678910111213141516171819202131 22 Carry flag Identification flag Virtual interrupt pending Virtual interrupt flag Alignment check Virtual 8086 mode Resume flag Nested task flag I/O privilege level Overflow flag Interrupt enable flag Direction flag Trap flag Sign flag Zero flag Auxiliary carry flag or adjust flag Parity flag
22
22 Data Transfer Instructions mov{b,w,l} source, dest General move instruction push{w,l} source pushl %ebx # equivalent instructions subl $4, %esp movl %ebx, (%esp) pop{w,l} dest popl %ebx # equivalent instructions movl (%esp), %ebx addl$4, %esp Many more in Intel manual (volume 2) Type conversion, conditional move, exchange, compare and exchange, I/O port, string move, etc. esp
23
23 Greater Detail on IA32 Assembly: Addressing Modes
24
24 Ways to Read and Write Data Processors have many ways to access data Known as “addressing modes” Two simplest ways (used in earlier example) Immediate addressing: movl $0, %ecx –Data embedded in the instruction –Initialize register ECX with zero Register addressing: movl %edx, %ecx –Data stored in a register –Copy value in register EDX into register ECX The others all deal with memory addresses To read and write data from main memory E.g., to get data from memory into a register E.g., to write data from a register back in to memory
25
25 Direct vs. Indirect Addressing Read or write from a particular memory location Essentially dereferencing a pointer Direct addressing: movl 2000, %ecx Address embedded in the instruction E.g., address 2000 corresponds to a global variable Load ECX register with the long located at address 2000 Indirect addressing: movl (%eax), %ebx Address stored in a register E.g., EAX register is a pointer Load EBX register with long located at address in EAX
26
26 More Complex Addressing Modes Base pointer addressing: movl 4(%eax), %ebx Extends indirect addressing by allowing an offset E.g., add “4” to the register EAX to get the address Allows access to a particular field in a structure E.g., if “age” starts at the 4 th byte of a record Indexed addressing: movl 2000(,%ecx,1), %ebx Starts from a base address (e.g., 2000) Adds an offset from a register (e.g., ECX) With a multiplier of 1, 2, 4, or 8 (e.g., 1 to multiply by 1) Allows register to be index for byte, word, or long array
27
27 Effective Address Displacement movl foo, %ebx Base movl (%eax), %ebx Base + displacement movl foo(%eax), %ebx movl 1(%eax), %ebx (Index * scale) + displacement movl (,%eax,4), %ebx Base + (index * scale) + displacement movl foo(%edx,%eax,4),%ebx eax ebx ecx edx esp ebp esi edi + 12481248 *+ None 8-bit 16-bit 32-bit Offset = Base Index scale displacement
28
28 Data Access Methods: Summary Immediate addressing: data stored in the instruction itself movl $10, %ecx Register addressing: data stored in a register movl %eax, %ecx Direct addressing: address stored in instruction movl 2000, %ecx Indirect addressing: address stored in a register movl (%eax), %ebx Base pointer addressing: includes an offset as well movl 4(%eax), %ebx Indexed addressing: instruction contains base address, and specifies an index register and a multiplier (1, 2, 4, or 8) movl 2000(,%ecx,1), %ebx
29
29 Layout of an Assembly Language Program
30
30 A Simple Assembly Program.section.text.globl _start _start: # Program starts executing # here # Body of the program goes # here # Program ends with an # “exit()” system call # to the operating system movl $1, %eax movl $0, %ebx int $0x80.section.data # pre-initialized # variables go here.section.bss # zero-initialized # variables go here.section.rodata # pre-initialized # constants go here
31
31 Main Parts of the Program Break program into sections (.section ) Data, BSS, RoData, and Text Starting the program Making _start a global (.global _start ) –Tells the assembler to remember the symbol _start –… because the linker will need it Identifying the start of the program ( _start ) –Defines the value of the label _start
32
32 Main Parts of the Program Exiting the program Specifying the exit() system call ( movl $1, %eax ) –Linux expects the system call number in EAX register Specifying the status code ( movl $0, %ebx ) –Linux expects the status code in EBX register Interrupting the operating system ( int $0x80 )
33
33 Conclusions Machine code Binary representation of instructions What operation to do, and on what data IA32 instructions Manipulate bytes, words, or longs Numerous kinds of operations Wide variety of addressing modes Next time Calling functions, using the stack
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.