代写代考 COMPARING 64-BIT INTEGERS

GET ANSWERS AT ESSAYLINK.NET

By default, statements are executed sequentially in both C and assembly:
statement statement … statement
Order of execution

Copyright By PowCoder代写 加微信 powcoder

However….
Decisions imply departure from sequential execution:
Then statement

HLL versus Assembly
• In assembly, all statements arranged vertically, so we rearrange the flowchart:
Then statement
Then statement

This arrangement requires a “goto”
FALSE test
if (test == FALSE) goto L1
Then-statement L1: ….
Then statement
This is similar to assembly, where decisions are implemented as a test followed by an instruction that branches (or not) depending on the result of the test.

COMPARE INSTRUCTIONS
Decisions in assembly require a two instruction sequence. A com- pare instruction compares two operands, followed by a conditional branch instruction that makes the actual decision to branch or not.
Instruction
CMP Rn,Op2
Updates flags N,Z,C and V
Compare Negative
CMN Rn,Op2
Op2 may be a small constant, a register, or a shifted register.
CMP is identical to the SUBS instruction, but discards the actual difference. The assembler automatically replaces CMP Rn,─constant by CMN Rn,constant

TEST INSTRUCTIONS
Instruction
TST Rn,Op2
Updates flags N and Z; if shifted, Op2 may affect C flag
Test Equivalence
TEQ Rn,Op2
TST is used to test whether any of the bits in Rn specified by Op2 are 1’s. TEQ is used to test whether Rn = Op2 without affecting the C or V flags.

CONDITIONAL BRANCH INSTRUCTIONS
If-then statement in C
Equivalent C code using goto and label
int32_t s32 ;
if (s32 > 10) then statement •••
int32_t s32 ;
if (s32 12 ? unsigned 01112 > 11002 ? (FALSE)
Signed and unsigned require different condition codes for magnitude comparisons!
HI (Higher Than)
HS (Higher Than or Same)
LO (Lower Than)
LS (Lower Than or Same)
EQ (Equal)
NE (Not Equal)
GT (Greater Than)
GE (Greater Than or Equal)
LT (Less Than)
LE (Less Than or Equal)
EQ (Equal)
NE (Not Equal)

CHOOSING THE CONDITION
You can’t use the same condition as used in C to control an if-statement.
If a condition doesn’t include the equality case, the opposite must. Vice-Versa!

COMPLETE CONDITION LIST
NE Not equal
GE Signed ≥ (“Greater than or Equal”) LT Signed (“Greater Than”)
LE Signed ≤ (“Less than or Equal”)
Z = 1 || N ≠ V
EQ and NE are same for
signed and unsigned.
HS (CS) LO (CC) HI
Unsigned ≥ (“Higher or Same”) or Carry Set C = 1 Unsigned (“Higher”)
Unsigned ≤ (“Lower or Same”) C = 0 || Z = 1
These are used for
Z = 0 && N = V
signed comparisons
Minus/negative
Plus – positive or zero (non-negative)
No overflow
AL Always (unconditional)
These are used for
C = 1 && Z = 0
unsigned comparisons
(Rarely used)
Never used. Included for completeness.

IF-THEN-ELSE SEQUENCES
C Source Code
Converted to goto
Assembly Code
int32_t s32 ;
if (s32 > 0)
then state
then statement
else statement
int32_t s32 ;
if (s32 0 ? LDR R0,[R0]
then statement
B L3 // skip over else!!
L3: … // end of the if
else statement
else statement
Without the unconditional branch, the else statement will always be executed!

IF-THEN-ELSE SEQUENCES
C Source Code
Assembly Code
int32_t s32 ;
if (s32 == 1)
else if (s32 == 2)
else if (s32 == 3)
statement #1
statement #1
statement #2
statement #2
statement #3
statement #3
LDR R0,=s32 // R0 0 ? CMP R0,1
statement #1
statement #1
// skip to end!
stateemmentt#2#2
L3: CMP R0,3 BNE L4
// skip to end!
statement #3
statement #3
// end of chain
These unconditional branches insure that only one statement is executed.

COMPARING 64-BIT INTEGERS
if (a64 > b64) then statement else Celse statement
LDR R0,=a64 // R0 b64
// a64 b64) then statement else C else statement
LDR R0,=a64 // R0 b64
// a64 0) b = 1 ; else c = 2 ;
The “IT Block”
LDR R0,[⋯]//Isa>0?
ITTEE GT //controls4instructions
LDRGT STR LDR STR
R0,=1 // yes: b  1 R0,[⋯]
R0,=2 // no: c2 R0,[⋯]
These suffixes specify the condition that enables the instruction and must match the condition code, the T’s and the E’s used in the IT instruction.
This specifies the condition that enables the next instruction.
Up to 3 additional instructions can be controlled by appending T’s or E’s.

A fast 64-bit unsigned max function
// uint64_t uMax64(uint64_t u1, uint64_t u2)
.global uMax64 .align .thumb_func
uMax64: SUBS
SBCS R12,R3,R1 // set flags, discard result
R12,R2,R0 // perform a 64-bit subtract
HI // if (u2 > u1) …
R1,R3 // then return u2 instead of u1 R0,R2 // by copying u2 into R1.R0
BX LR // return .end

Using IT Blocks to Reduce Code Size
Instructions within an IT block never affect the flags – ADD is 32-bits (4 bytes) outside of an IT block
– ADD is 16-bits (2 bytes) inside an IT block
(Assembler replaces ADD inside IT block with ADDS since the ‘S’ has no effect and the ADDS allows a 16-bit representation.)
16 instruction bytes
10 instruction bytes
ADD R0,R0,R1 // 4 bytes ADD R0,R0,R1 // 4 bytes ADD R0,R0,R1 // 4 bytes ADD R0,R0,R1 // 4 bytes
ITTTT AL ADDAL R0,R0,R1 ADDAL R0,R0,R1 ADDAL R0,R0,R1 ADDAL R0,R0,R1
// 2 bytes // 2 bytes // 2 bytes // 2 bytes // 2 bytes

COMPOUND CONDITIONALS
• Example: if (x > 10 && x 10) if (x = 20) goto L2 ; } } y= 0 ;

COMPOUND LOGICAL OR
if (x +100)
then statement
if (x +100) goto L1 ; goto L2 ;
then statement
One condition per statement, each controlling a goto.
When you see this pattern, the last goto can be eliminated.
C statements now easily map 1-to-1 into assembly
LDR R0,[⋯] // R0 = –100 && x = –100 && x +100) goto L1 ;
then statement
Separate the conditions as we did before.
C statements now easily map 1-to-1 into assembly
LDR R0,[⋯] // R0 +100) BGT L1 // goto L1
then statement
if (x +100) goto L1 ;
then statement

COMPOUND CONDITIONALS
Summary with else statements
Compound Conditional in C
Rewritten with goto’s and labels
Assembly language version
if (x > 0 && x = 9) goto L1 ;
then statement
else statement
LDR R0,[⋯]//R0x CMP RO,0
then statement
else statement
if (x == 0 || x == 9)
then statement
else statement
if (x == 0) goto L1 ;
if (x != 9) goto L2 ;
then statement else statement
LDR R0,[⋯]//R0x CMP R0,0
then statement
else statement

Another Approach: Compound AND
if (x >= –100 && x +100)
then statement
(1) Initial flowchart
(2) Layout flowchart
boxes vertically.
LDR R0,[⋯] CMP R0,-100 BLT L1
CMP R0,+100 BLE L2
then statement
+100? BLE L2 L1: Yes
then statement
(3) Identify needed labels
(3) Identify conditional branch instructions
(5) Translate to assembly
then-statement
then statement

WRITING LOOPS
Instruction
Compare and Branch if Zero
CBZ Rn,label
Branch to label If Rn=0
Compare and Branch if Non-Zero
CBNZ Rn,label
Branch to label If Rn≠0
CMP R0,0 BEQ L1
CMP R0,0 BNE L1
Sometimes used at the top of a while or for loop
Sometimes used at the bottom of a do-while loop

WRITING LOOPS
// uint32_t Factorial(uint32_t n)
.global Factorial .align .thumb_func
Factorial:
done: MOVS.N R0,R1 BX LR
Loop initialization
Test for completion
MOVS.N R1,1 // initialize: Factorial = 1
CBZ R0,done // check n: Done if n is 0
MULS.N R1,R1,R0 // factorial *= n
SUBS.N R0,R0,1 // decrement n and update flags B top // branch to top of loop
Body of the loop
// leave result in R0 // return
Unconditional branch to repeat the loop.

WRITING LOOPS
// uint32_t gcd(uint32_t u1, uint32_t u2)
.global gcd
.align Test for completion .thumb_func
// This loop needs no initialization
CMP R0,R1 // continue? (u1 != u2) BEQ done
ITE HI // loop body and update: SUBHI R0,R0,R1 // u1 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com

The post 代写代考 COMPARING 64-BIT INTEGERS appeared first on PowCoder代写.

GET ANSWERS AT ESSAYLINK.NET

代写代考 COMPARING 64-BIT INTEGERS
Scroll to top