CS计算机代考程序代写 assembler x86 js assembly Lecture Topics

Lecture Topics
• Continue x86 instructions
• Conditional codes
• Control flow instructions
• Assembler conventions
• Code example

• PS1 (Problem Set 1) posted – Due by 2/8
– PS1 must be done in at least groups of 4.
• Only one person in this group should submit the completed problem set with a proper partners.txt (as indicated in the instructions).
Aministrivia

Condition Codes (in EFLAGS)
• Among others (not mentioned in this class)…
SF: sign flag: result is negative when viewed as 2’s complement data type
ZF: zero flag: result is exactly zero
CF: carry flag: unsigned carry or borrow occurred
(or other, instruction-dependent meaning, e.g., on shifts)
OF: overflow flag: 2’s complement overflow (and other instruction-dependent meanings)
PF: parity flag: even parity in result (even # of 1 bits)
© Steven Lumetta, Zbigniew Kalbarczyk ECE391

What Instructions Set Flags (condition codes)?
• Not all instructions set flags
• Some instructions set some flags!
• Use CMP or TEST to set flags:
CMPL %EAX, %EBX # flags (EBX – EAX) TESTL %EAX, %EBX # flags (EBX AND EAX)
• Note that EBX does not change in either case
• What combinations of flags are needed for unsigned/signed relationships comparator?
© Steven Lumetta, Zbigniew Kalbarczyk ECE391

Control Flow Instructions (1)
• Consider two three-bit values A and B; How to decide if A
signed jne jl jle je jge jg
• forms shown are those used when disassembling
– do not expect binary to retain your version – e.g., “jnae” becomes “jb”
© Steven Lumetta, Zbigniew Kalbarczyk ECE391

• Other branches
Other Control Instructions
– jo — jump on overflow (OF) – jp — jump on parity (PF)
– js — jump on sign (SF)
– jmp — unconditional jump
• Control instructions: subroutine call and return
CALL printf CALL *%EAX CALL *(%EAX)
RET
© Steven Lumetta, Zbigniew Kalbarczyk
# (push EIP), EIP  printf
# (push EIP), EIP  EAX
# (push EIP), EIP  M[EAX]
# EIP  M[ESP], ESP  ESP + 4 ECE391

Stack Operations
• Push and pop supported directly by x86 ISA
PUSHL %EAX POPL %EBP PUSHFL POPFL
# M[ESP – 4]  EAX, ESP  ESP – 4 #EBPM[ESP],ESPESP+4
# M[ESP – 4]  EFLAGS, ESP  ESP – 4 # EFLAGS  M[ESP], ESP  ESP + 4
© Steven Lumetta, Zbigniew Kalbarczyk
ECE391

Data Size Conversion
• These instructions extend 8- or 16-bit values to 16- or 32-bit values
• General form
MOV
S—sign extend Z—zero extend
• Examples
MOVSBL %AH, %ECX MOVZWL 4(%EBP), %EAX
to type from type
© Steven Lumetta, Zbigniew Kalbarczyk
ECE391
# ECX  sign extend to 32-bit (AH)
# EAX  zero extend to 32-bit (M[EBP + 4])

Assembler Conventions
label: requires a colon, and is case-sensitive (unlike almost anything else in assembly)
# comment to end of line /* C-style comment
… (can consist of multiple lines) */
; command separator (NOT a comment as in LC-3)
.string “Hello, world!”, “me” .byte 100, 0x30, 052 .word …
.long …
.quad … .single … .double …
# NUL-terminated
# integer constants of various sizes
# floating-point constants
• If assembly file name ends in .S (case-sensitive!), file is first passed through © StevCen’sLumpertetap, ZrboigcniewsKsaolbrarc(z#ykdefine and #EiCnEc3l9u1 de)

Code Example
• Given: EBX pointing to an array of structures with ECX elements in the array
• Find: min and max age START
the structure
char* name long age
ESI  0
EDX  large # EDI  small #
init vars
find min/max
ESI ≥ ECX ?
N
Y
loop over elements
END
• first, define registers
– ESI — index into array – EAX—currentage
– EDX — min age seen
– EDI — max age seen
• next, use systematic decomposition…
compare one age
ESI  ESI + 1

Code Example – Loop Design Process (1)
• What is the task to be repeated?
– update the min/max ages
– based on the age of a single person in the array
• What are the invariants?
– ESI = array index of person being considered
– EDX = min age of those earlier in array
– EDI = max age of those earlier in array
– ECX = size of array
• What are the stopping conditions?
– reach the end of the array – i.e., ESI ≥ ECX
© Steven Lumetta, Zbigniew Kalbarczyk ECE391

Code Example – Loop Design Process (2)
• What must be done when a stopping condition is met? – nothing! (by definition of this particular problem)
• How do we prepare for the first iteration?
– set array index to point to first person
– set min age to something large (or to first person’s, but may not exist)
– set max age to something small
• How do we prepare for subsequent iterations?
– update min/max age based on current person – incrementESI
© Steven Lumetta, Zbigniew Kalbarczyk ECE391

Code Example – Loop Body
N EAX>Y max ?
N EAX< Y min ? © Steven Lumetta, Zbigniew Kalbarczyk ECE391 EAX  age compare one age (empty) max  EAX (empty) min  EAX LOOP: © Steven Lumetta, Zbigniew Kalbarczyk ECE391 Code Example – Assembly (1) XORL %ESI,%ESI MOVL TWO_MM,%EDX MOVL TWO_MM+4,%EDI CMPL %ECX,%ESI JGE DONE # read the age using one memory reference. MOVL 4(%EBX,%ESI,8),%EAX CMPL %EDI,%EAX # check the max. age JLE NOT_MAX MOVL %EAX,%EDI # init vars # loop test NOT_MIN: DONE: TWO_MM: .LONG 0x7FFFFFFF, 0x80000000 CMPL %EDX,%EAX JGE NOT_MIN MOVL %EAX,%EDX INCL %ESI JMP LOOP # more code ... # check the min age © Steven Lumetta, Zbigniew Kalbarczyk ECE391 Code Example – Assembly (2) NOT_MAX: # loop update