Generalities for Assembly Language

47
1 ©These slides may be freely used, distributed, and incorporated into other works. Generalities for Assembly Language 1 item per line of source code directive (declaration) instruction Comments are always allowed, but the syntax varies from one architecture to another Macro substitution like the #define from C no macros in MIPS assembly language!

description

Generalities for Assembly Language. 1 item per line of source code directive (declaration) instruction Comments are always allowed, but the syntax varies from one architecture to another Macro substitution like the #define from C no macros in MIPS assembly language!. - PowerPoint PPT Presentation

Transcript of Generalities for Assembly Language

Page 1: Generalities for Assembly Language

1©These slides may be freely used, distributed, and incorporated into other works.

Generalities for Assembly Language

1 item per line of source code directive (declaration) instruction

Comments are always allowed, but the syntax varies from one architecture to another

Macro substitution like the #define from C no macros in MIPS assembly language!

Page 2: Generalities for Assembly Language

2©These slides may be freely used, distributed, and incorporated into other works.

Comments in MIPS assembly language

# Here is my comment.

# NO spanning lines!

# Everything to the right of the

# pound character to the end of the

# line is a comment.

Page 3: Generalities for Assembly Language

3©These slides may be freely used, distributed, and incorporated into other works.

Declarations

The format of the syntax:[label] type [initial value]

type is one of.byte for a single, character-sized variable.word for a single, integer-sized variable.float for a single-precision-sized variable

shows optional item

Page 4: Generalities for Assembly Language

4©These slides may be freely used, distributed, and incorporated into other works.

Declaration Examples:

a_char: .byte 'a' # single quotes

count: .word

MAX: .word 1000 # decimal value

mask: .word 0x34006700 # hexadecimal

X: .word # initial value of 0

char: .byte # initial value is

# NULL character

.word # initial value is 0,

# but how to access?

e: .float 2.71828 # IEEE single

# precision

Page 5: Generalities for Assembly Language

5©These slides may be freely used, distributed, and incorporated into other works.

More Declaration Examples:

# an array of 5 characters, initialized

letters: .byte 'a', 'b', 'c', 'd', 'e'

# 2 labels for the exact same variable

count1:

count2: .word 0

# letter case is distinguished!

# 2 separate variables are declared

MAX: .word 1000

max: .word 6

Page 6: Generalities for Assembly Language

6©These slides may be freely used, distributed, and incorporated into other works.

Yet More Declaration Examples:

# strings go in double quote marks

err_msg: .asciiz "integer too large\n"

no_null: .ascii "non null terminated"

xyznull: .asciiz "xyz" # 4 chars

xyz: .ascii "xyz" # 3 chars

Page 7: Generalities for Assembly Language

7©These slides may be freely used, distributed, and incorporated into other works.

Directives

A way to give information to the assembler.

All directives start with '.' (on many architectures).

MIPS examples:.byte # space for 1 character-sized

# variable

.word # space for 1 integer-sized

# variable

.float # space for 1 single precision-

# sized variable

Page 8: Generalities for Assembly Language

8©These slides may be freely used, distributed, and incorporated into other works.

More Directives Examples

.data # identifies the start of the # declaration section # There can be more than 1 .data # section in a program. # There will be 1 global section # where all data is placed.

.text # identifies the start of a section # of code # There can be more than 1 .text # section in a program.

Page 9: Generalities for Assembly Language

9©These slides may be freely used, distributed, and incorporated into other works.

Yet More Directives Examples

.asciiz # Places a null-terminated string # into memory. # A string consists of

# consecutively allocated

# characters.

.ascii # Places a string into memory, # without null termination.

Page 10: Generalities for Assembly Language

10 ©These slides may be freely used, distributed, and incorporated into other works.

MIPS R2000 register usage syntax: $regnumber

examples: $3, $18, $20 all 32 registers are both general

purpose, yet have specific uses. . .$0 always the value 0$1 used by the assembler for

MAL->TAL translation$2, $3 function return values$4 - $7 parameters

$26, $27 used by the operating system$29 stack pointer$31 return address register

10

Page 11: Generalities for Assembly Language

11 ©These slides may be freely used, distributed, and incorporated into other works.

Which registers to use?

$0 for the immediate value 0 $8 - $25 for all variables

When writing code, use registers as needed.

Karen’s most excellent advice: Document what is in each register as you

write code (not after the code is completed) !

11

Page 12: Generalities for Assembly Language

12 ©These slides may be freely used, distributed, and incorporated into other works.

d must designate a registers1, s2 may designate a register or

be an immediater1, r2 must designate a register

Only 1 immediate operand per instruction! (for common sense reasons)

3

Page 13: Generalities for Assembly Language

13 ©These slides may be freely used, distributed, and incorporated into other works.

Some arithmetic/logical instructionsmove d, s1 d = s1

add d, s1, s2d = s1 + s2; two's complement

addu d, s1, s2 d = s1 + s2; unsigned

sub d, s1, s2d = s1 – s2; two's complement

subu d, s1, s2 d = s1 – s2; unsigned

mul d, s1, s2d = s1 * s2; two's complement

div d, s1, s2 d = s1 / s2; two's complement; gives quotient

divu d, s1, s2 d = s1 / s2; unsigned; gives quotient

Page 14: Generalities for Assembly Language

14 ©These slides may be freely used, distributed, and incorporated into other works.

More arithmetic/logical instructions

rem d, s1, s2 d = s1 / s2; two's complement; gives remainder

remu d, s1, s2 d = s1 / s2; unsigned; gives remainder

and d, s1, s2 d = s1 & s2; bitwise AND

or d, s1, s2 d = s1 | s2; bitwise OR

not d, s1d = ~s1; bitwise complement

nand d, s1, s2 d = s1 NAND s2; no C equivalent

nor d, s1, s2 d = s1 NOR s2; no C equivalent

xor d, s1, s2 d = s1 ^ s2; bitwise XOR

Page 15: Generalities for Assembly Language

15 ©These slides may be freely used, distributed, and incorporated into other works.

More arithmetic/logical instructions

rol d, s1, s2 d = rotate left of s1 by s2 places

ror d, s1, s2 d = rotate right of s1 by s2 places

sll d, s1, s2 d = logical left shift of s1 by s2 places

sra d, s1, s2 d = arithmetic right shift of s1 by s2 places

srl d, s1, s2 d = logical right shift of s1 by s2 places

Page 16: Generalities for Assembly Language

16 ©These slides may be freely used, distributed, and incorporated into other works.

#C code

#

# aa = bb + cc + (dd – ee);

16

Page 17: Generalities for Assembly Language

17 ©These slides may be freely used, distributed, and incorporated into other works.

MIPS Load/Store Instructions

mnemonic operands operationlw d, addr one word is loaded from

addr and placed into d; addr must be

word aligned

lb d, addr one byte is loaded from addr and placed

into the rightmost byte of d; sign extension defines the other bits of d

4

Page 18: Generalities for Assembly Language

18 ©These slides may be freely used, distributed, and incorporated into other works.

more MIPS Load/Store Instructions

mnemonic operands operationlbu d, addr one byte is loaded from

addr and placed into the rightmost byte of d; zero extension defines the other bits of dli d, immed the immediate value is

placed into d

sw d, addr a word in d is stored to addr; addr must

be word aligned18

Page 19: Generalities for Assembly Language

19 ©These slides may be freely used, distributed, and incorporated into other works.

more MIPS Load/Store Instructions

mnemonic operands operationsb d, addr the byte in the rightmost byte of d is stored to addr

la d, label the address assigned for label is placed into d

19

Page 20: Generalities for Assembly Language

20 ©These slides may be freely used, distributed, and incorporated into other works.

loads l_ $_, addr

stores s_ $_, addr

addr is one of1. label2. ($_)3. displacement($_)

Page 21: Generalities for Assembly Language

21 ©These slides may be freely used, distributed, and incorporated into other works.21

memory

X ////////

1. addr is a label

Example:

lw $8, X

////////$8

Page 22: Generalities for Assembly Language

22 ©These slides may be freely used, distributed, and incorporated into other works.22

memory

*** ////////

2. addr is ($_)

the address is in a registerExample:

lw $10, ($14)

////////$10

***$14

Page 23: Generalities for Assembly Language

23 ©These slides may be freely used, distributed, and incorporated into other works.23

memory

1004 ////////

3. addr is displacement($_)

an address is in a registerExample:

lw $12, 4($15)

////////$12

1000$15

+

Page 24: Generalities for Assembly Language

24 ©These slides may be freely used, distributed, and incorporated into other works.24

Dealing with bytes example:

lb $20, Xmemory

X

sign extend

$20

1 byte

Page 25: Generalities for Assembly Language

25 ©These slides may be freely used, distributed, and incorporated into other works.25

memory

Y:

$21 00000000 00000000 00000000 ////////

////////

1 byte

Zero Extend

More dealing with bytes:

lbu $21, Y

Page 26: Generalities for Assembly Language

26 ©These slides may be freely used, distributed, and incorporated into other works.26

Example:

sb $13,X

$13 XXXXX

X XXXXX

1 byte

memory

Only 1 byte of memory changes

Page 27: Generalities for Assembly Language

27 ©These slides may be freely used, distributed, and incorporated into other works.27

Example:

la $22, X

X

memory

Much like int X; int *pX;

px = &X;

in $22

$22 X

Page 28: Generalities for Assembly Language

28 ©These slides may be freely used, distributed, and incorporated into other works.

Some branch instructions

b labelunconditional branch to label

beq r1, r2, labelbranch to label if (r1) == (r2)

bne r1, r2, labelbranch to label if (r1) != (r2)

bgt r1, r2, labelbranch to label if (r1) > (r2)

bge r1, r2, labelbranch to label if (r1) >= (r2)

blt r1, r2, labelbranch to label if (r1) < (r2)

ble r1, r2, labelbranch to label if (r1) <= (r2)

Page 29: Generalities for Assembly Language

29 ©These slides may be freely used, distributed, and incorporated into other works.

More branch instructions

beqz r1, labelbranch to label if (r1) == 0

bnez r1, labelbranch to label if (r1) != 0

bgtz r1, labelbranch to label if (r1) > 0

bgez r1, labelbranch to label if (r1) >= 0

bltz r1, labelbranch to label if (r1) < 0

blez r1, labelbranch to label if (r1) <= 0

Page 30: Generalities for Assembly Language

30 ©These slides may be freely used, distributed, and incorporated into other works.

2 more control instructions

j label unconditional jump to label

jal labelunconditional jump to label,with return address in $31

Page 31: Generalities for Assembly Language

31 ©These slides may be freely used, distributed, and incorporated into other works.

Good examples of branch instructions: beq $8, $9, end_program

bgtz $16, next_check

Incorrect, bad, and wrong: beq X, $12, bad_code

Also wrong:ble $10, -1, less_than_case

Correct implementation of the ble:li $11, -1

ble $10, $11, less_than_case

31

Page 32: Generalities for Assembly Language

32 ©These slides may be freely used, distributed, and incorporated into other works.

#C code

#

# if ( x == 0 ) {

# y++;

# }

32

Page 33: Generalities for Assembly Language

33 ©These slides may be freely used, distributed, and incorporated into other works.

#C code

#

# if ( x == y ) {

# a++;

# y = a – 3;

# }

33

Page 34: Generalities for Assembly Language

34 ©These slides may be freely used, distributed, and incorporated into other works.

# C code

#

# sum = 0

# for ( i = 0; i < count; i++ ) {

# sum = sum + i;

# }

34

Page 35: Generalities for Assembly Language

35 ©These slides may be freely used, distributed, and incorporated into other works.

# C code

# #define MAX 13

# count = 0;

# while ( count < MAX ) {

# /* code missing! */

# }

35

Page 36: Generalities for Assembly Language

36 ©These slides may be freely used, distributed, and incorporated into other works.

# C code

# while ( (x >= y) && (z < 300) ) {

# z = z – x;

# if ( (x % 2) == 0 ) {

# x--;

# y++;

# }

# }

36

Page 37: Generalities for Assembly Language

37 ©These slides may be freely used, distributed, and incorporated into other works.

I/O instructions

putc r1print the ASCII character represented by the least significant byte of r1

getc r1read a character, placing it in the least significant byte of r1

puts r1/label

print the null-terminated string that begins at the address within r1 or at the address given by label

Page 38: Generalities for Assembly Language

38 ©These slides may be freely used, distributed, and incorporated into other works.

# code

getc $21

# execution waits for user input of a

# character; once the user types an 'R':

$21 0x?? 0x?? 0x?? 0x52

38

Page 39: Generalities for Assembly Language

39 ©These slides may be freely used, distributed, and incorporated into other works.

# code

putc $15

$15 0x52 0x53 0x54 0x55

outputU

^

more output goes here, when there is more output

39

Page 40: Generalities for Assembly Language

40 ©These slides may be freely used, distributed, and incorporated into other works.

.datastr1: .asciiz "hello.".text

puts str1

hello. ^ more output starts here,

when more is printed

40

Page 41: Generalities for Assembly Language

41 ©These slides may be freely used, distributed, and incorporated into other works.

.datastr1: .asciiz "hello.".text

la $12, str1 # address of first # character in stringputs $12 # address of first # character to print # is in $12

hello. ^ more output starts here, when more is printed

41

Page 42: Generalities for Assembly Language

42 ©These slides may be freely used, distributed, and incorporated into other works.

.datastr1: .asciiz "hello.\nMy name is George.".text

puts str1

hello. My name is George. ^ more output starts here, when more is printed

42

Page 43: Generalities for Assembly Language

43 ©These slides may be freely used, distributed, and incorporated into other works.

.datastr1: .ascii "Hi.\n"str2: .asciiz "I am a badger.".text puts str1

Hi. I am a badger. ^ more output starts here, when more is printed

43

Page 44: Generalities for Assembly Language

44 ©These slides may be freely used, distributed, and incorporated into other works.

# MAL program to print out the alphabet

.datastr1: .asciiz "The alphabet:\n"

# register assignments# $8 -- the ASCII character code# to be printed# $9 -- the ASCII code for 'z', the# ending character

44

Page 45: Generalities for Assembly Language

45 ©These slides may be freely used, distributed, and incorporated into other works.

.text__start: la $10, str1 puts $10 add $8, $0, 97 # $8 gets ASCII for 'a'; # could be li $8, 97 add $9, $0, 122 # $9 gets ASCII for 'z'; # could be li $9, 122while: bgt $8, $9, all_done putc $8 add $8, $8, 1 b whileall_done: li $10, '\n' putc $10 done

45

Page 46: Generalities for Assembly Language

46 ©These slides may be freely used, distributed, and incorporated into other works.

# this simple MAL program reads in 2# characters, figures out which one is# alphabetically first, and prints it out.

# register assignments# $8 -- the first character typed by the user# $9 -- the second character typed in# by the user# $10 -- temporary# $11 -- holds the value of the larger# character# $13 -- the address of the newline# character constant# $14 -- newline character (a constant)

46

Page 47: Generalities for Assembly Language

47 ©These slides may be freely used, distributed, and incorporated into other works.

.text__start: getc $8 # get 2 characters getc $9

li $14, '\n' # print newline putc $14 # figure out which is larger sub $10, $9, $8 bgez $10, secondlarger add $11, $8, $0 b printresult

secondlarger: add $11, $9, $0

printresult: putc $11 done

47