CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term...

42
CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 1

Transcript of CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term...

Page 1: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

CS Basics6) Bits and Branching

Emmanuel BenoistFall Term 2016-17

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 1

Page 2: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Bits and Branching

� Functions on bitsShiftsRotate

� Example : hexdump1

� Flags tests and branchesComparisons

� Memory Addressing

� Conclusion

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 2

Page 3: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Functions on bits

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 3

Page 4: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Functions on bits

Binary boolean functions1

Work on a Bit-by-Bit basis (similar to one of our exercises)AND

True if and only if both operands are trueOR

True if and only if at least one of the operands is trueXOR

True if and only if just one of the operands is true

Unary boolean functionNOT change a value into its opposite.

1More on this in the course “Descrete mathematics”Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 4

Page 5: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Masking out bits

AND can be used to isolate bitsWe use and to set all unwanted bits to 0

PrincipleWe use a bit mask: the interesting bits are 1, other are 0We make a ANDIn the result, all the uninteresting bits are removed, we canread interesting bits directly

ExampleWe are interested in bits 0 and 3 in a byte (we start countingfrom the left with number 0)The mask is 0000 1001 (noted 09H)If we are interested by the bit 3 and 0 of register AL (8 bitgeneral purpose register)and al,09h

All the unitersting bits are 0Values of bit 0 and 3 remain, the rest is erased

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 5

Page 6: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

An AND instructionChapter 9 ■ Bits, Flags, Branches, and Tables 283

1 0 0

0

1

1

1

0

0

1

0

0

0

1

1

0

0

0

0

0

1

0

0

0

AND

AND

AND

AND

AND

AND

AND

AND

=

=

=

=

=

=

=

=

AND AL, BL

AL : 9DH10011101

BL : 30H00110000

After Execution:AL : 10H

00010000

Value Mask Result

LSB

MSB

Figure 9-2: The anatomy of an AND instruction

The OR InstructionClosely related to the AND logical operation is OR, which, like the AND logicaloperation, has an embodiment with the same name in the x86 instruction set.Structurally, the OR instruction works identically to AND. Only its truth table isdifferent: While AND requires that both its operands be 1 for the result to be 1,OR is satisfied that at least one operand has a 1 value. The truth table for OR isshown in Table 9-3.

Table 9-3: The OR Truth Table for Assembly Language

BIT 1 OPERATOR BIT 2 RESULT BIT

0 OR 0 0

0 OR 1 1

1 OR 0 1

1 OR 1 1

Because it’s unsuitable for isolating bits, OR is used much more rarelythan AND.

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 6

Page 7: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Shifts

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 7

Page 8: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Shift bits

Shift bits to one side or the otherSHL SHifts bits to the LeftSHR SHifts bits to the Right

Syntax

shl <register/memory>, count

The first operand : target of the operationThe second operand : the number of bits by which to shift

ExampleAX contains B76FH =1011 0111 0110 1111BSHL AX,1

AX becomes 6EDEH =0110 1110 1101 1110BOne bit has been “forgotten”It has been transfered in the “Carry Flag”

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 8

Page 9: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Rotate

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 9

Page 10: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

The Rotate instructions

To reuse the forgotten bits: RotateRCL, RCR, ROL and ROR are rotate functions

Rotate Left ROL

The bits reappear on the other side10110010becomes 01100101

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 10

Page 11: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

The rotate instruction

288 Chapter 9 ■ Bits, Flags, Branches, and Tables

The Rotate InstructionsThat said, if a bit’s destiny is not to be lost in cosmic nothingness, you need touse the rotate instructions RCL, RCR, ROL, and ROR instead. The rotate instructionsare almost identical to the shift instructions, but with a crucial difference: abit bumped off one end of the operand reappears at the opposite end of theoperand. As you rotate an operand by more than one bit, the bits marchsteadily in one direction, falling off the end and immediately reappearing atthe opposite end. The bits thus ‘‘rotate’’ through the operand as the rotateinstruction is executed.

Like so many things, this shows better than it tells. Take a look atFigure 9-4. The example shown here is the ROL (Rotate Left) instruction,but the ROR instruction works the very same way, with the bits moving in theopposite direction. An initial binary value of 10110010 (0B2h) is placed in AL.When an ROL AL,1 instruction is executed, all the bits in AL march toward theleft by one position. The 1-bit in bit 7 exits AL stage left, but runs around andreappears immediately from stage right.

0 1 1 0 0 1 0 1

The binary value 10110010 in AL before ROL AL,1:

ROL shifts all bits left and moves bit 7 to bit 0.What was 10110010 is now 01100101.

1 0 1 1 0 0 1 0

Bit 0

Figure 9-4: How the rotate instructions work

Again, ROR works exactly the same way, but the movement of bits is fromleft to right instead of (as with ROL) right to left. The number of bits by whichan operand is rotated can be either an immediate value or a value in CL.

There is a second pair of rotate instructions in the x86 instruction set: RCR(Rotate Carry Right) and RCL (Rotate Carry Left). These operate as ROL and ROR

do, but with a twist: The bits that are shifted out the end of an operand andreenter the operand at the beginning travel by way of the Carry flag. The paththat any single bit takes in a rotate through CF is thus one bit longer than itwould be in ROL and ROR. I’ve shown this graphically in Figure 9-5.

As with the shift instructions, there’s no advantage to rotating a value bymore than 31 bits. (If you rotate a value by 32 bits, you end up with the same

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 11

Page 12: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Rotate Carry

Functions RCL and RCR

The forgotten bit goes in the carryThen it is added on the other side

Chapter 9 ■ Bits, Flags, Branches, and Tables 289

1 0 1 1 0 0 1 0 0

The binary value 10110010 in AL before RCL AL,1:

RCL shifts all bits left and moves bit 7 to the Carry flag.The 0-bit previously in the Carry flag is moved into bit 0.

CF

0 1 1 0 0 1 010

CFBit 0

Figure 9-5: How the rotate through carry instructions work

value in the operand that you started with.) The rotate instructions bump bitsoff one end of the operand and then feed them back into the opposite endof the operand, to begin the trip again. If you mentally follow a single bitthrough the rotation process, you’ll realize that after 32 rotations, any givenbit is where it was when you started rotating the value. What’s true of one bitis true of them all, so 31 rotations is as much as will be useful on a 32-bit value.This is why, in protected mode programming (and on the old 286 as well), theshift-by count is truncated to 5 bits before the instruction executes. After all,the largest value expressible in 5 bits is . . . 32!

Setting a Known Value into the Carry FlagIt’s also useful to remember that previous instructions can leave values inCF, and those values will be rotated into an operand during an RCL or RCR

instruction. Some people have the mistaken understanding that CF is forcedto 0 before a shift or rotate instruction, which is not true. If another instructionleaves a 1-bit in CF immediately before an RCR or RCL instruction, that 1-bitwill obediently enter the destination operand, whether you want it to or not.

If starting out a rotate with a known value in CF is important, there is apair of x86 instructions that will do the job for you: CLC and STC. CLC clearsthe Carry flag to 0. STC sets the Carry flag to one. Neither instruction takes anoperand, and neither has any other effects beyond changing the value in theCarry flag.

Bit-Bashing in Action

As you saw in earlier chapters, Linux has a fairly convenient method fordisplaying text to your screen. The problem is that it only displays text—if

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 12

Page 13: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Functions to set the Carry Flag

We can set the carry flagCLC Clear carry flagSTC Set carry flag

Can be useful to set the value before a rotatate carryfunction

The value can be set to 1Or to 0

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 13

Page 14: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Example : hexdump1

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 14

Page 15: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Example Program

Display the content of a binary fileTransform Bit and Bites into charactersSimilar to a read only Hex editor

Standard flowsInput file : standard inputUsage: hexdump1 < file.bin

Output on standard output

What it doesRead 16 bytes at a timeTransform each byte in hexPrint out the 16 hexadecimal values separated by spaces

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 15

Page 16: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

hexdump1 I

; .....; Run it this way:; hexdump1 < (input file);SECTION .bss ; Section containing uninitialized data

BUFFLEN equ 16 ; We read the file 16 bytes at a timeBuff: resb BUFFLEN ; Text buffer itself

SECTION .data ; Section containing initialised dataHexStr: db ” 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00”,10HEXLEN equ $−HexStrDigits: db ”0123456789ABCDEF”

SECTION .text ; Section containing codeglobal start ; Linker needs this to find the entry point!start:

nop ; This no−op keeps gdb happy...; Read a buffer full of text from stdin:Read:

mov eax,3 ; Specify sys read callmov ebx,0 ; Specify File Descriptor 0: Standard Inputmov ecx,Buff ; Pass offset of the buffer to read tomov edx,BUFFLEN ; Pass number of bytes to read at one passint 80h ; Call sys read to fill the buffermov ebp,eax ; Save # of bytes read from file for latercmp eax,0 ; If eax=0, sys read reached EOF on stdinje Done ; Jump If Equal (to 0, from compare)

....

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 16

Page 17: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

hexdump1 II

; Set up the registers for the process buffer step:mov esi,Buff ; Place address of file buffer into esimov edi,HexStr ; Place address of line string into edixor ecx,ecx ; Clear line string pointer to 0

; Go through the buffer and convert binary values to hex digits:Scan:

xor eax,eax ; Clear eax to 0

; Here we calculate the offset into the line string, which is ecx X 3mov edx,ecx ; Copy the pointer into line string into edx

; shl edx,1 ; Multiply pointer by 2 using left shift; add edx,ecx ; Complete the multiplication X3

lea edx,[edx∗2+edx]; Get a character from the buffer and put it in both eax and ebx:

mov al,byte [esi+ecx] ; Put a byte from the input buffer into almov ebx,eax ; Duplicate the byte in bl for second nybble

; Look up low nybble character and insert it into the string:and al,0Fh ; Mask out all but the low nybblemov al,byte [Digits+eax] ; Look up the char equivalent of nybblemov byte [HexStr+edx+2],al ; Write the char equivalent to line string

; Look up high nybble character and insert it into the string:shr bl,4 ; Shift high 4 bits of char into low 4 bitsmov bl,byte [Digits+ebx] ; Look up char equivalent of nybblemov byte [HexStr+edx+1],bl ; Write the char equivalent to line string

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 17

Page 18: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

hexdump1 III

; Bump the buffer pointer to the next character and see if we re done:inc ecx ; Increment line string pointercmp ecx,ebp ; Compare to the number of characters in the bufferjna Scan ; Loop back if ecx is <= number of chars in buffer

; Write the line of hexadecimal values to stdout:mov eax,4 ; Specify sys write callmov ebx,1 ; Specify File Descriptor 1: Standard outputmov ecx,HexStr ; Pass offset of line stringmov edx,HEXLEN ; Pass size of the line stringint 80h ; Make kernel call to display line stringjmp Read ; Loop back and load file buffer again

; All done! Let s end this party:Done:

mov eax,1 ; Code for Exit Syscallmov ebx,0 ; Return a code of zeroint 80H ; Make kernel call

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 18

Page 19: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Lookup table294 Chapter 9 ■ Bits, Flags, Branches, and Tables

'1'

MOV AL, BYTE [Digits+EAX]

Digits + EAX

Digits '0'

'3'

'2'

'5'

'4'

'7'

'6'

'9'

'8'

'B'

'A'

'D'

'C'

'F'

'E'

AL: 7 (07H) AL: '7' (37H)

Note: Here, 'Digits' is the addressof a 16-byte table in memory

Before After

Figure 9-6: Using a lookup table

As with most of assembly language, everything here depends on memoryaddressing. The first hex digit character in the lookup table is at the address inDigits. To get at the desired digit, we must index into the lookup table. We dothis by adding an offset into the table to the address inside the brackets. Thisoffset is the nybble in AL.

Adding the offset in AL to the address of Digits (using EAX) takes us rightto the character that is the ASCII equivalent of the value in AL. I’ve drawn thisout graphically in Figure 9-6.

There are two possibly confusing things about the MOV instruction thatfetches a digit from Digits and places it in AL:

We must use EAX in the memory reference rather than AL, because ALcannot take part in effective address calculations. Don’t forget that AL is

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 19

Page 20: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

HexStr296 Chapter 9 ■ Bits, Flags, Branches, and Tables

6 C 4 9 F E 7 0

Entry 0 Entry 1 Entry 2 Entry 15

+0 +1 +2 +0 +1 +2 +0 +1 +2 +0 +1 +2

HexStr HexStr + 3 HexStr + 6 HexStr + 45

HexStr + (3 × Entry #) + 2

HexStr + (3 × 2) + 2

HexStr + 8

Figure 9-7: A table of 16 three-byte entries

by three. I’ve already described the MUL instruction, which handles arbitraryunsigned multiplication in the x86 instruction set. MUL, however, is very slowas instructions go. It has other limitations as well, especially the ways in whichit requires specific registers for its implicit operands.

Fortunately, there are other, faster ways to multiply in assembly, with just alittle cleverness. These ways are based on the fact that it’s very easy and veryfast to multiply by powers of two, using the SHL (Shift Left) instruction. It maynot be immediately obvious to you, but each time you shift a quantity one bitto the left, you’re multiplying that quantity by two. Shift a quantity two bitsto the left, and you’re multiplying it by four. Shift it three bits to the left, andyou’re multiplying by eight, and so on.

You can take my word for it, or you can actually watch it happen in asandbox. Set up a fresh sandbox in Kate and enter the following instructions:

mov al,3shl al,1shl al,1shl al,2

Build the sandbox and load the executable into Insight. Set the EAX displayfield in the Registers view to Decimal. (This must be done after the sandboxprogram is running, by right-clicking on the EAX field and selecting Decimalfrom the context menu.) Then step through the instructions, watching thevalue of EAX change in the Registers view for each step.

The first instruction loads the value 3 into AL. The next instruction shifts ALto the left by one bit. The value in AL becomes 6. The second SHL instructionshifts AL left by one bit again, and the 6 becomes 12. The third SHL instructionshifts AL by two bits, and the 12 becomes 48. I’ve shown this graphically inFigure 9-8.

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 20

Page 21: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Multiplying by shiftingChapter 9 ■ Bits, Flags, Branches, and Tables 297

0 0 0 0 0 0 1 1

0 0 0 0 0 1 1 0

0 0 0 0 1 1 0 0

Value = 3

Value = 6

Value = 12

SHL AL,1

SHL AL,1

SHL AL,2

0 0 1 1 0 0 0 0 Value = 48

Figure 9-8: Multiplying by shifting

What if you want to multiply by 3? Easy: you multiply by 2 and then addone more copy of the multiplicand to the product. In the hexdump1 program,it’s done this way:

mov edx,ecx ; Copy the character counter into edxshl edx,1 ; Multiply pointer by 2 using left shiftadd edx,ecx ; Complete the multiplication X3

Here, the multiplicand is loaded from the loop counter ECX into EDX. EDXis then shifted left by one bit to multiply it by 2. Finally, ECX is added once tothe product EDX to make it multiplication by 3.

Multiplication by other numbers that are not powers of two may be done bycombining a SHL and one or more ADDs. To multiply a value in ECX by 7, youwould do this:

mov edx,ecx ; Keep a copy of the multiplicand in ecxshl edx,2 ; Multiply edx by 4add edx,ecx ; Makes it X 5add edx,ecx ; Makes it X 6add edx,ecx ; Makes it X 7

This may look clumsy, but remarkably enough, it’s still faster than usingMUL! (There’s an even faster way to multiply by 3 that I’ll show you a little laterin this chapter.)

Once you understand how the string table HexStr is set up, writing the hexdigits into it is straightforward. The least-significant hex digit is in AL, and the

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 21

Page 22: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Multiply by shifting and adding

Multiply by two

mov al,3

shl al,1

shl al,1

shl al,2

AL is 48 = 3*2*2*4

Multiply by 3Multiply x by two and add once x

mov rdx, rcx ; copy the number

shl rdx, 1 ; multiply by two

add rdx, rcx ; add x*2 and x

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 22

Page 23: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Multiply

Multiply by 7

mov rdx, rcx

shl rdx,2 ; edx multiplied by 4

add rdx, rcx ; edx is value times 5

add rdx, rcx ; times 6

add rdx rcx ; times 7

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 23

Page 24: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Flags tests and branches

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 24

Page 25: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Jumps

Unconditional jumpsGo to an address in any caseJMP

jmp <label>

Conditional jumpsTests the value of one or more flags before jumpingotherwise it goes to the next instruction

Test the Zero Flag (ZF)JNZ Jump Non Zero (seen previousely)JZ Jump if ZF is set

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 25

Page 26: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Jumps (example)

Jump and jump zero

mov word [Runningsum],0 ; Clear the running total

mov rcx, 17 ; We will loop 17 times

WorkLookup:

add word [Runningsum],3 ; Add 3 to the running total

dec rcx ; substract one to the counter

jz SomewereElse ; If the counter is zero, we are done↘

→!

jmp WorkLookup ; We loop otherwise

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 26

Page 27: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Comparisons

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 27

Page 28: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Comparisons

Flags are used to comparateWe use the instruction CMP

It comparates two operandsand sets the following flags: OF, SF, ZF, AF, FFBasically a substraction, where the result is thrown awayJust the flags remain

cmp <op1>, <op2> ; sets OF, SF, ZF, AF, PF and CF

; Equivalent to

; Result = <op1> - <op2>

; sub <op1>,<op2>

; Where result is forgotten

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 28

Page 29: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Comparisons (Cont.)Provides the following testings

Equal (ZF is set),Not equal (ZF is not set),Greater than (ZF is not set, SF is not set),Less than (ZF is not set, SF is set),Greater than or equal to (ZF is set or SF is not set),and Less than or equal to

Equality testing jumpsJE: Jump if Equal, JNE: Jump not equal

Jumps for Singed ValuesGreater than / less thanJG: Jump if Greater; JGE: Jump if Greater or EqualJNG, JNGEJL, JLE, JNL,JNLE

Jumps for Unsigned ValuesAbove / belowJA: Jump is Above; JAE: Jump is Above or EqualJNA, JNAEJB, JBE, JNB, JNBE

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 29

Page 30: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Test one bit

We are interested in testing one bitsTEST operation tests elements bitwiseIt executes a AND, sets the flags and discard the result

test <operand>, <bit mask>

test ax, 08h ; Bit 3 in binary is 0000 1000B

AX is not changed (unlike with AND)

If bit 3 is a 1Zero Flag is 0

If bit 3 is a 0Zero Flag is 1

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 30

Page 31: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Bit Test

One can test a bit with BT

The bit is copied in the Carry Flag CF

bt <value containing bit>, <bit number>

TestingTest the value of CF JC (Jump if Carry) and JNC (Jump ifNot Carry)

bt eax, 4 ; Test bit 4 of AX

jnc quit ; We are done if bit 4 = 0

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 31

Page 32: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Memory Addressing

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 32

Page 33: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Protected Mode Memory Addressing308 Chapter 9 ■ Bits, Flags, Branches, and Tables

BASE + (INDEX × SCALE) + DISP.

Any GP Register Any GP Register 1, 2, 4, or 8(1 does nothing)

Any 32-bitconstant

The scale is applied to the index before the additions are done.

Figure 9-9: Protected mode memory addressing

The displacement may be any 32-bit constant. Obviously, 0, while legal,isn’t useful.

The scale must be one of the values 1, 2, 4, or 8. That’s it! The value 1 islegal but doesn’t do anything useful, so it’s never used.

The index register is multiplied by the scale before the additions are done.In other words, it’s not (base + index) × scale. Only the index register ismultiplied by the scale.

All of the elements are optional and may be used in almost any combina-tion.

16-bit and 8-bit registers may not be used in memory addressing.

This last point is worth enlarging upon. There are several different ways youcan address memory, by gathering the components in the figure in differentcombinations. Examples are shown in Table 9-8.

Effective Address CalculationsEach of the rows in Table 9-8 summarizes a method of expressing a memoryaddress in 32-bit protected mode. All but the first two involve a little arithmeticamong two or more terms within the brackets that signify an address. Thisarithmetic is called effective address calculation, and the result of the calculationis the effective address. The term is ‘‘effective address’’ in that it means thataddress that will ultimately be used to read or write memory, irrespective ofhow it is expressed. Effective address calculation is done by the instruction,when the instruction is executed.

The effective address in the Base scheme is simply the 32-bit quantity storedin the GP register between the brackets. No calculation is involved, but whatyou see in the source code is not a literal or symbolic address. So although the

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 33

Page 34: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Protected Mode Memory addressing

Effective Address CalculationsThe address used to read or write in memory

Base schemeThe quantity inside a register contains the address

mov [eax], 42 ; Moves 42 into the memory which ↘

→address is in eax

DisplacementsSymbolic address defined in .bss or .data

mov eax, [HexStr] ; HexStr is a label pointing ↘

→somewhere in the memory

mov ebx, [HexStr + 3] ; Displacement of 3 bytes ↘

→starting at HexStr

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 34

Page 35: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Memory addressing (Cont.)

Base + Displacement Addressing

mov byte [HexStr+edx+2], al ; Copy one byte ↘

→stored in al into memory

HexStr+2 is computed at compilation,so we have: Base (eax) and displacement (HexStr)

Base + Index AddressingAddress is calculated based on two registers, one is the base(stable), the other the index (incremented)

sub byte [ebp+ecx], 20h ; substract 20h from one ↘

→byte in memory

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 35

Page 36: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Loop visiting memory

; Set up the registers for the process buffer step:

mov ecx,esi ; Place the number of bytes read into ecx

mov ebp,Buff ; Place address of buffer into ebp

dec ebp ; Adjust count to offset

; Visit buffer, convert lowercase to uppercase chars:

Scan:

cmp byte [ebp+ecx],61h ; Test input char against ↘

→lowercase ’a’

jb Next ; If below ’a’ in ASCII, not lowercase

cmp byte [ebp+ecx],7Ah ; Test input char against ↘

→lowercase ’z’

ja Next ; If above ’z’ in ASCII, not lowercase

; At this point, we have a lowercase char

sub byte [ebp+ecx],20h ; Subtract 20h to give ↘

→uppercase...

Next: dec ecx ; Decrement counter

jnz Scan ; If characters remain, loop back

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 36

Page 37: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Memory Addressing (Cont.)

Index x scale + displacement addressingWhen the data is an arrayscale = size of one dataIf we read 4 bytes at a time, we visit only addresses separatedby 4 bytes

mov ecx, 0

MyLoop:

add ecx,4

mov edx, [Digits + ecx] ; 4 bytes in memory ↘

→are transfered at once

.....

jnz MyLoop ; end of loop

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 37

Page 38: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

How address scaling works

We have a label DDTable on a table of double words (dd)

Chapter 9 ■ Bits, Flags, Branches, and Tables 313

Typically, the scale term is the size of the individual elements in the table.If your table consists of 2-byte word values, the scale would be 2. If yourtable consists of 4-byte double word values, the scale would be 4. If your tableconsists of 8-byte quad word values, the scale would be 8.

The best way to explain this is with a diagram. In Figure 9-10, we’reconfronted with the address [DDTable + ECX*4]. DDTable is a table of doubleword (32-bit) values. DDTable’s address is the displacement. The ECX registeris the index, and for this example it contains 2, which is the number of the tableelement that you want to access. Because it’s a table of 4-byte double words,the scale value is 4.

[DDTable + ECX * 4]

2Index Value:

0 0 0 0 0 0 0 0 0 0 0 0

Displacement (Address of DDTable in Memory)

Effective Address of Table Element #2

+0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11

Table Element#0

Table Element#1

Table Element#2

Scale Value:

X

+

(in a GP Register like ECX)

=

4 (One of the literal constants 2, 4, or 8)

Figure 9-10: How address scaling works

Because each table element is four bytes in size, the offset of element #2 fromthe start of the table is 8. The effective address of the element is calculated byfirst multiplying the index by the scale, and then adding the product to theaddress of DDTable. There it is!

Other Addressing SchemesAny addressing scheme that includes scaling works just this way. The differ-ences lie in what other terms are figured into the effective address. The Base +Index × Scale scheme adds a scaled index to a base value in a register ratherthan a displacement:

mov ecx,2 ; Index is in ecx

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 38

Page 39: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Load Effective Address LEAIt is sometime useful to store an effective address in aregister

To access it later

lea ebx, [ScaleValue+ecx*4] ; stores the address ↘

→in ebx

LEA does not store the value, only the address!Can be missued to do some calculation (not on address)The two following codes are equivalent (they multiply edx by 3)

mov edx, ecx ; copy the number

shl edx, 1 ; multiply by two

add edx, ecx ; add x*2 and x

And

lea edx, [edx*2+edx] ; Multiply edx by 3

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 39

Page 40: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Conclusion

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 40

Page 41: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Conclusion

Manipulating bitsManipulate data as arrays of bitsUsing boolean functions (AND, OR, XOR or NOT)Using shifts (or rotate)

Example: hexdump1Transforms binary input into a readable fileAll values are written in hexadecimal

JumpsLot of different jumpsDepend on testingOn Flags (Zero, Carry, ...)On comparisons of numbers (>, <, ≥, leq)

Memory addressingMust respect schemesIs very convenient to access memory

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 41

Page 42: CS Basics 6) Bits and Branching€¦ · CS Basics 6) Bits and Branching Emmanuel Benoist Fall Term 2016-17 Berner Fachhochschule jHaute cole spcialise bernoise jBerne University of

Bibliography

This course corresponds to the chapter 9 of the course book:Assembly Language Step by Step (3rd Edition)

Berner Fachhochschule | Haute cole spcialise bernoise | Berne University of Applied Sciences 42