Publications: 82 | Followers: 0

Getting Started with Assembly Language - cefns.nau.edu

Publish on Category: Birds 0

Deeper Assembly:Addressing, Conditions,Branching, and Loops
Some material taken from Assembly Language for x86 Processors by Kip Irvine © Pearson Education, 2010Slides revised 2/13/2014 by Patrick Kelley
2
Overview
Data Transfer InstructionsAddition and SubtractionIndirect AddressingJMP and BRANCH Instructions
Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, 2010.
3
Data Transfer Instructions
Operand TypesDirect Memory Operandsmove/load/store InstructionsZero & Sign Extension
4
Operand Types
Imm(immediate) – a constant valueLabel – address specified by a labelR(t,s,d) – Contents of a register(R) – Contents of memory pointed to by a register.offset(R) – Contents pointed to by R + offsetExamples:li $v0, 0x4f #Imminto register $v0la $a0,mynum# label into register $a0lw$ra, 12($sp) # data at $sp+ 12 into $ra
5
Direct Memory Operands
MIPS, unlike some architectures, does not support direct memory addressingInstead you must set up your memory accesses.Point to the data by loading the address into a register: la $a0,myVarNow you can accessmyVarwith an offset to the address:lw$a1, 0($a0) # loadmyVarinto a1Note that it was Ok that the offset was 0, but we could also use a non-zero offset if we needed, for instance to access elements of an array.
6
Move, Load, and Store
Load and Store are the memory access operationsThere are several variations of each based on data size and type. (lb,lbu,lwl,sh,sw, etc.)There are also load variations that don’t affect data directly (la, li)Move is used to transport data between registers only and never affects memoryVariations on Move are use to access special registers.
7
Zero- and Sign- extension
Extension is used when a small piece of data is stored into a larger space. (ex. byte into word.)The space to the left is filled with either zero or the sign bit.Address operations sign extend the offset beforeORingit to the base.Arithmetic operations select which to use by type:Unsigned operations zero-extendSigned operations sign-extend
8
What's Next
Data Transfer InstructionsAddition and SubtractionIndirect AddressingJMP and BRANCH Instructions
9
ADD and SUB Instructions
add oradduRd,Rs,RtLogic:destinationsourceRs+ sourceRtsub orsubuRd,Rs,RtLogic:destinationsourceRs–sourceRtaddioraddiuRd,Rs,ImmLogic:destinationsourceRs+ immediateNote that memory is not affected by these operations
10
Negate
negorneguRd,RsLogic:destination0 -sourceRsnegudoes not give exception if you try to negate maximum negativeSubtract operations are really a convenience, not needed since you can negate and add a number to get the sameeffect.However, negate in MIPS is implemented by doing a subtraction, so it actually would add a step.
11
What's Next
Data Transfer InstructionsAddition and SubtractionIndirect AddressingJMP and BRANCH Instructions
12
Indirect Addressing
Recall that some operations take an offset and base as an operandWe can store the base address of a data structure, like an array, in a register.Then we can refer to individual elements by adding an offset to the base address.This works well when offsets are known and the elements are few, as in stack frames.But since the offset must be a constant, it is cumbersome to use for long lists.An example is on the following slide:
13
# TITLE Add and Subtract (AddSub2.s)# This program adds and subtracts 32-bit integers.# It uses offsets to get to the variables.data# variablesNums: .word 0x1000.word 0x5000.word 0x3000Sum: .word 0.text.globlmainmain: # start of the main procedurela $a0,Nums# Put the base address in $a0lw$t0, 0($a0) # Put first number into $t0lw$t1, 4($a0) # Put second number into $t1lw$t2, 8($a0) # Put third number into $t2add $t4, $t0, $t1 # Add first two numbers, put in $t4sub $t4, $t4, $t2 # Subtract third number from resultsw$t4, Sum # Put result in sumjr$ra# return to caller (exit program)
Example: Using indirect addressing
14
Indirect Addressing(cont.)
A better way is to iterate through a list by adding to the base address.If the list contains complex data, we can create a list of base addresses (pointers) and iterate through those.Using base addresses as simple pointers (without an offset) also works well for structures like linked lists and queues.
15
What's Next
Data Transfer InstructionsAddition and SubtractionIndirect AddressingJMP and BRANCH Instructions
16
Program Flow
A processor does operations sequentiallyThe Fetch – Decode – Execute cycle gets instructions in orderIf we wish to change the program flow, we need a special instructionJump instructions are unconditionalEquivalent to a ‘goto’ statement.Used to call subroutines or functionsMay jump to a label or address in registerBranch instructions are tied to a conditionSimilar in effect to an ‘if – then – else’ constructAlways branch to a label
Jump Instruction
top:..jtop
Jis an unconditional jump to a label that is usually within the same procedure.Syntax:jLabelLogic:IRLabelExample:
17
18
Conditional Branching
In some processors, each instruction sets status flags in the processorUsually in a status register with a bit for each flagTypical flags are sign, carry, overflow, zero, and parityThere is a certain efficiency in that we don’t have to test a result if the flags are already setEx:movAX, 5sub AX, 5jzgoHereIfZero… # else continue on
19
Conditional Branching(cont.)
MIPS tests conditions as part of the Branch instructionNo need for the overhead of maintaining status flagsAlso fewer instructions to needed in the architectureBut branch instructions take a little longer because they have to do the test firstEx:li $S1, -5add $S1, $S1, 5beq$zero, $S1,goHereIfZero…# else continueon
20
Applications(1 of 5)
bgtu$V0, $V1, Larger
Task: Jump to a label ifunsigned$V0is greater than$V1Solution: Usebgtu
bgt$V0, $V1, Larger
Task: Jump to a label ifsigned$V0is greater than$V1Solution: Usebgt
21
Applications(2 of 5)
lw$V1, Val1bleu $V0, $V1, L1#below or equal
Jump to label L1 ifunsigned$V0is less than or equal to Val1
lw$V1, Val1ble$V0, $V1,L1 #below or equal
Jump to label L1 ifsigned$V0isless than or equal to Val1
22
Applications(3 of 5)
sw$V1, Largebleu $V0, $V1, Nextsw$V0,LargeNext:
Compare unsigned$V0to$V1,and copy the larger of the two into a variable namedLarge
sw$V0, Smallbge$V0, $V1, Nextsw$V1,SmallNext:
Compare signed$V0to$V1,and copy the smaller of the two into a variable namedSmall
23
Applications(4 of 5)
lw$V0, ($a0)beq$V0, $0, L1**OR**beqz$V0, L1
Jump to label L1 if the memory word pointed to by$A0equals Zero
lw$V0, ($a0)andi$V0, $V0, 1beqz$V0,L2
Jump to label L2 if thewordin memory pointed to by$A0is even
24
Applications(5 of 5)
li $S1,0x0000000Band $S0, $S0, $S1; clear unwanted bitsbeq$S0, $S1, L1; check remainingbits
Task: Jump to label L1 if bits 0, 1, and 3 in$S0areall set.Solution: Clear all bits except bits 0, 1,and 3. Then compare the result with0000000B in hex.
25
Encrypting a String (do until)
# The encryption key is 232, can be any byte value# We’ll use an arbitrary string size of 128.dataBuffer: .space 129bufSize: .word 128.textlw$a0,bufSize# loop counterla $a1, Buffer # point to bufferL1:lbu$a3, ($a1) # get a bytexori$a3, $a3, 232#translate abytesb$a3, ($a1) # store translated byteaddi$a0, $a0, -1 # decrement loop counterbeqz$a0, Next # if done, leave loopaddi$a1, $a1, 1#point to next bytej L1 # loopNext:
The following loop uses the XOR instruction to transform every character in a string into a new value.
26
Encrypting a String (while, for)
# The encryption key is 232, can be any byte value# We’ll use an arbitrary string size of 128.dataBuffer: .space 129bufSize: .word 128.textlw$a0,bufSize# loop counterla $a1, Buffer # point to bufferL1:blez$a0,Next # if done, leave looplbu$a3, ($a1) # get a bytexori$a3, $a3, 232#translate abytesb$a3, ($a1) # store translated byteaddi$a0, $a0, -1 # decrement loop counteraddi$a1, $a1, 1#point to next bytej L1 # loopNext:
The following loop uses the XOR instruction to transform every character in a string into a new value.
27
Block-Structured IF Statements
Assembly language programmers can easily translate logical statements written in C++/Java into assembly language. For example:
lw$a0, op1lw$a1, op2bne$a0, $a1, L1li $a3, 1sw$a3, XjL2L1: li$a3,2sw$a3, XL2:
if( op1 == op2 )X = 1;elseX = 2;
28
Compound Expression with AND(1 of 3)
When implementing the logical AND operator, consider that HLLs use short-circuit evaluationIn the following example, if the first expression is false, the second expression is skipped:
if (al >bl) AND (bl> cl)X = 1;
29
Compound Expression with AND(2 of 3)
# al,bl, and cl are in $a0, $a1, and $a2#repsectively; $a3 holds 1bgt$a0, $a1, L1#first expression...jnextL1:bgt$a1, $a2, L2#second expression...jnextL2:#both are truesw$a3, X#set X to 1next:
if (al >bl) AND (bl> cl)X = 1;
This is one possible implementation . . .
*Note that the branches could have beenbgtu.
30
Compound Expression with AND(3 of 3)
# al,bl, and cl are in $a0, $a1, and $a2#repsectively; $a3 holds 1ble$a0, $a1, next#first expression...#quit if falseble$a1, $a2, next#second expression...#quit if falsesw$a3, X#both are truenext:
if (al > bl) AND (bl > cl)X = 1;
But the following implementation uses40%less code by reversing the first relational operator. We allow the program to "fall through" to the second expression:
31
Compound Expression with OR(1 of 2)
When implementing the logical OR operator, consider that HLLs use short-circuit evaluationIn the following example, if the first expression is true, the second expression is skipped:
if (al > bl) OR (bl > cl)X = 1;
32
Compound Expression with OR(2 of 2)
# al,bl, and cl are in $a0, $a1, and $a2#repsectively; $a3 holds 1bgt$a0, $a1, L1#isal>bl?bgt$a1, $a2, next#no: is BL > CL?jbenext#no: skip next statementL1:sw$a3, X#set X to 1next:
We can use "fall-through" logic to keep the code as short as possible:
if (al >bl) OR (bl> cl)X = 1;

0

Embed

Share

Upload

Make amazing presentation for free
Getting Started with Assembly Language - cefns.nau.edu