Download CSIS1120A - 11. Assembly Language Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Compiler wikipedia , lookup

Subroutine wikipedia , lookup

Abstraction (computer science) wikipedia , lookup

Object-oriented programming wikipedia , lookup

Functional programming wikipedia , lookup

Programming language wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Reactive programming wikipedia , lookup

Go (programming language) wikipedia , lookup

Structured programming wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Assembly language wikipedia , lookup

Transcript
CSIS1120A
11. Assembly Language Programming
CSIS1120A
11. Assembly Language Programming
1
Assembly Language Programming
Use MIPS instruction set as example.
There are some assembly language instructions in MIPS
that are not part of the instruction set. The assembler
actually convert them into machine instructions. (This is
quite special to MIPS)
The following tables shows the instruction that can be used
in MIPS assembly language programs. Instructions with a
”pointing finger” are synthesized by the assembler.
CSIS1120A
11. Assembly Language Programming
2
MIPS Assembly Language Programming
CSIS1120A
11. Assembly Language Programming
3
MIPS Assembly Language Programming
CSIS1120A
11. Assembly Language Programming
4
MIPS Assembly Language Programming
CSIS1120A
11. Assembly Language Programming
5
MIPS Addressing Formats
The CPU supports only one addressing mode —
displacement mode.
The assembler, again, can synthesize some additional
addressing modes to present more traditional addressing
capabilities to the assembly language programmer.
e.g. Register indirect addressing (Rn), and absolute
addressing A, is converted by the assembler to
displacement mode.
CSIS1120A
11. Assembly Language Programming
6
Assembler Directives
Also called pseudo opcodes, which are not to be translated
into machine instruction. They tell the assembler to do
something instead.
The following tables show some useful assembler
directives.
CSIS1120A
11. Assembly Language Programming
7
Assembler Directives
Directives
.data
Description
Tells the assembler to add all subsequent data to the data section.
.end [proc name]
Sets the end of a procedure.
.ent proc name
Sets the beginning of the procedure
proc name. (Entry point)
.frame
Describe a stack frame. The first regframe reg,
ister is the frame register; offset is the
offset,
distance from the frame register to the
return pc register virtual frame pointer; the second register is the return PC.
.globl name
Makes name external.
nop
Tells the assembler to put in an instruction that has no effect on the machine state (e.g. SLL R0,R0,0)
CSIS1120A
11. Assembly Language Programming
8
Assembler Directives
.space
expression
.text
.word exp1
[,exp2] · · ·
[,expN]
CSIS1120A
Reserves spaces, amount specified
by the value of expression in bytes.
The assembler fills the space with zeros.
Tells the assembler to add subsequent code to the text section (i.e.
program section)
Truncates expressions in the commaseparated list to 32 bits and put them
in successive locations.
11. Assembly Language Programming
9
Some Notes
Register n is denoted by $n
The register 0, $0 always contain 0. You cannot modify it.
You can compare a pair of registers by BEQ and BNE
instruction.
The destination register is the 1st operand, instead of the
last operand, e.g. add $1, $2, $3 means $1=$2+$3.
CSIS1120A
11. Assembly Language Programming
10
Control Structures
If-then-else construct
Example:
if (a[0]>a[1]) x=a[0];
else x=a[1];
CSIS1120A
11. Assembly Language Programming
11
ALP
.data
#
a:
.word 1
#
.word 3
#
x:
.word 4
#
.text
#
.globl main
.ent main
main:
la $8, a
lw $9, 0($8)
lw $10, 4($8)
bgt $9,$10,f1
sw $10, x
j f2
f1: sw $9, x
f2: j $31
.end main
CSIS1120A
data segment
create storage containing 1
create storage containing 3
create storage containing 4
program segment
#
#
#
#
#
#
#
#
$8 = address of a
$9 = a[0]
$10= a[1]
if $9>$10, goto f1
x=$10
goto f2
x=$9
return
11. Assembly Language Programming
12
Repetition construct
Example:
a=0;
for (i=0; i<10; i++) a+=i;
CSIS1120A
11. Assembly Language Programming
13
ALP
a:
.data
.word
0
.text
.ent main
main:
move $8, $0
li
$9, 0xa
move $10, $0
li
$11,1
f1: add $8, $8, $10
add $10,$11,$10
bne $9,$10 f1
sw $8,a
j $31
.end main
CSIS1120A
#
#
#
#
$0 is always 0
$9=10, no. of iterations
$10, loop counter
load immmediate, $11=1
# if ($9!=$10) goto f1
# a=$8
# return
11. Assembly Language Programming
14
While-construct
Example:
temp=0;
a=1;
while (temp < 100) {
temp+=a;
a++;
}
CSIS1120A
11. Assembly Language Programming
15
ALP
# fill in the .data, .text part as in
# previous examples
move $8, $0
# $8 is temp, $8=0
li
$9, 1
# $9 is a, $9=1
move $10,$9
# $10=$9
li $11,0x64
# $11=100
f1: add $8, $8, $9 # temp+=a
add $9, $10, $9 # a++
blt $8, $11,f1 # if ($8<$11) goto f1
CSIS1120A
11. Assembly Language Programming
16
Function Calls
Use jump and link (jal) instruction for function calls,
return address stored in $31.
You need to specifies the input parameters and the output
parameters for your function.
If you change any values of the registers, you need either
to spell out in the program specification, or push the
original value, and pop it out at the end of the function call.
In particular, if you call other functions (thus changing
$31), you have to store $31 to the stack.
CSIS1120A
11. Assembly Language Programming
17
Example:
Complex Number Addition and Multiplication
ar:
ai:
br:
bi:
cr:
ci:
dr:
di:
CSIS1120A
.data
.word
.word
.word
.word
.word
.word
.word
.word
1
5
2
3
0
0
0
0
# real part of a
# imaginary part of a
# b
# c
# d
11. Assembly Language Programming
18
.text
.globl main
.ent main
main: sw $31, 0($sp)
add $sp,$sp,-4
lw $8, ar
lw $9, ai
lw $10, br
lw $11, bi
jal cadd
sw $12, cr
sw $13, ci
jal cmult
sw $12, dr
sw $13, ci
add $sp,$sp,4
lw $31, ($sp)
j $31
CSIS1120A
# push $31, because call
# another function
# setup parameters for cadd
# call cadd
# store result
# call cmult
# store result
# restore $31, return addr
# return
11. Assembly Language Programming
19
cadd: #complex add subroutine
#input parameter ($8,$9) ($10,$11)
#output parameter($12,$13)
add $12, $8, $10
add $13, $9, $11
j $31
CSIS1120A
#return
11. Assembly Language Programming
20
cmult: #complex multiplication
#input parameter ($8,$9) ($10,$11)
#output parameter ($12, $13)
#push $14, save register 14
sw $14, ($sp)
add $sp, $sp, -4
mul $12, $8, $10
#$12=mul real part
mul $14, $9, $11
#$14=mul img part
sub $12, $12, $14
#result.real
mul $13, $8, $11
#imaginary parts
mul $14, $9, $10
add $13, $13, $14
add $sp, $sp, 4
# restore $14
lw $14, ($sp)
j $31
# return
CSIS1120A
11. Assembly Language Programming
21
Example
Convert all characters into upper case letters.
.data
a:
.asciiz "This is a test"
# zero-terminated string
.text
.globl main
.ent main
main: sw
add
move
CSIS1120A
$31, 0($sp)
$sp,$sp,-4
$9, $0
#push $31
#$9=0
11. Assembly Language Programming
22
loop: lb
$10, a($9)
beq $10,$0,exit
jal capitalize
sb
$10, a($9)
add $9,$9,1
j
loop
exit: add $sp,$sp,4
lw $31,0($sp)
j
$31
CSIS1120A
#
#
#
#
#
#
#
load byte
$10==0? end of str
call capitalize
store result back
incr $9, next char
goto loop
restore $31
# return
11. Assembly Language Programming
23
Capitalize:
#input is $10 ,output is $10, if $10 is lower
#case letter, change to upper case
ret1:
CSIS1120A
sw
add
sw
add
li
li
blt
bgt
sub
add
lw
add
lw
j
$8, 0($sp)
$sp,$sp,-4
$9, 0($sp)
$sp,$sp,-4
$8, 0x61
$9, 0x7a
$10, $8, ret1
$10, $9, ret1
$10, $10,0x20
$sp, $sp, 4
$9, 0($sp)
$sp, $sp, 4
$8, 0($sp)
$31
11. Assembly Language Programming
#push $8
#push $9
#$8=’a’
#$9=’z’
#0x20=’a’-’A’
#pop $9
#pop $8
#return
24