Assembly Language
CS2253 Owen Kaser, UNBSJ
Assembly Language CS2253 Owen Kaser, UNBSJ Assembly Language Some - - PowerPoint PPT Presentation
Assembly Language CS2253 Owen Kaser, UNBSJ Assembly Language Some insane machine-code programming Assembly language as an alternative Assembler directives Mnemonics for instructions Machine-Code Programming (or, Why Assemblers
CS2253 Owen Kaser, UNBSJ
– Put the constant 0 into R1 – Put the constant 10 into R2 – Add R1 and R2, put the result into R1 – Subtract the constant 1 from R2 and set the status flags – If the Z flag is not set, reset the PC to contain the
address of the 3rd instruction above.
itself, or EOR a register with itself, or... let's use Move.
1110 00 1 1101 0 0000 0001 0000 00000000 = 0xE3A01000
.
1110 00 1 1101 0 0000 0010 0000 00001010 = 0xE3A0200A
fields that make up a machine instruction, let's make a program do that.
individual instructions, which will be given human friendly names (mnemonics).
various other directives (aka pseudo-ops) that control how the assembler does its job.
subsequent linker program, if you write a multi-module program.
– an optional label in column 1 – an optional instruction or directive (and any arguments) – an optional comment (after a ; )
mymain mov r1,#0 ← mymain is the label mov is the instruction # precedes the constant ; nice comment, eh? mov r2,#10 ; put 10 into r2 (bad comment) myloop add r1, R1, r2 ← case insensitive for reg names subs r2, r2, #1 ← final s means to affect flags bne myloop ← condition is “ne” (z flag false) sticky b sticky ← so we don't fall out of pgm end ← directive to assembler: you're done ;don't use “end”; it seems to be buggy in Crossware
– set aside memory space for variables/arrays – define a block of code or data – give a symbolic name to a value
specified number of bytes of memory. These locations will be initialized to 0.
allocated memory.
– myarray SPACE 100 – myarr2 SPACE 100*4 ←constant expression's ok
v1 DCB 10 v2 DCW 20 v3 DCB 30 v4 DCD 40 If v1 is at address 3000, then v2 starts at 3002 (1 byte of padding) v3 is at 3004 v4 starts at 3008 (3 bytes padding)
v1 DCB 10 v2 DCWU 20 v3 DCB 30 v4 DCDU 40 If v1 is at 3000, then v2 starts at 3001 v3 is at 3003 v4 starts at 3004 (aligned by luck)
names the first of them.
values. DCB “XY” is same as DCB 'X','Y' or DCB 88,89
blocks of data and several blocks of code. And it can be written in several different source-code files.
give it a new name and specify its type.
– eg AREA fred,code – You can go back to a previous area by using an old name
various sections (and any library routines you need) into a single program.
AREA mycode,code foo add R1, R2, R3 add R4, R5, #10 AREA mydata, data var1 dcb “cs2253” AREA mycode ← continues mycode where it left off add R6, R7, R8 This feature allows for us to show our data declarations near the code that uses them (maybe good software engineering), even if the different sections end up being far apart in memory. Memory picture on board...
AREA mycode, CODE starthere add R1, R2, R3 DCD 0x1234567 ; this line is fishy add R2, R3, R4 AREA mydata, DATA var1 DCD 1234 var2 add R2, R3, R4 ; this line is also fishy var3 DCB “hello world”,0
fred DCB 20, 200, “Frederick Wu” fred_age EQU fred+0 fred_height EQU fred+1 fred_name EQU fred+2 Subsequent instructions can load data from fred_height rather than the more cryptic fred+1. But to the assembler, both loads will be equivalent.
documentation, so maybe they exist under a different name :(
– ENTRY – RN – LTORG, though we do have the “LDR rx,=” construct (eg
textbook page 72)
– SETS
– add – sub – b – mov
– RSB – reverse subtract – ADC, SBC – add/subtract with carry – RSC – reverse subtract with carry – MVN – move “negative” (a bitwise NOT) – AND, ORR, EOR, BIC – bitwise logical operations – MUL, SMULL, UMULL – various * ops – MLA, SMLAL, UMLAL – multiply/accumulate.
temp = v1; v1 = v2; v2 = temp;
mov r3, r1 mov r1, r2 mov r2, r3
eor r1, r1, r2 eor r2, r1, r2 eor r1, r1, r2
– so is 0xAB0 ( 0xAB with a 28 bit rotate right) – so is 0xB000000A (0xAB with a 4-bit rotate right)
– LSR (logical shift right) – ASR (arithmetic shift right) – ROR (rotate right) – RXX (33 bit ROR using carry between MSB and LSB)
– Either as a constant (“immediate”) – Or by the least significant 5 bits of a register
– Bit 4 distinguishes the cases – Bits 5 & 6 say what kind of shift/rotate – Bits 11 to 7 involve which register, or the constant
R2
– Bits 27, 26, 25 and 4 are all 0
S=1 means to set the flags
to set flags: they don’t change any of R0 to R15.
MOV R1,#0 ; The sum MOV R2,#1 LP ADD R1, R1, R2 ADD R2, R2, #1 CMP R1, R4 ; computes R1 – R4, sets flags BLS LP ; LS = unsigned Lower or Same (CF=0 or Z=1) ; use LE for signed Lesser or Equal
– Should product be 32 bits or 64 bits? – Are the input values considered signed?
value in a pair of registers.
(R1, R2) ← (R1, R2) + R3*R4 with unsigned math
– Above, R1 is the least significant 32 bits