Generalities for assembly language
This presentation is the property of its rightful owner.
Sponsored Links
1 / 47

Generalities for Assembly Language PowerPoint PPT Presentation


  • 68 Views
  • Uploaded on
  • Presentation posted in: General

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!.

Download Presentation

Generalities for Assembly Language

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Generalities for assembly language

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!


Comments in mips assembly language

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.


Declarations

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


Declaration examples

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


More declaration examples

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


Yet more declaration examples

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


Directives

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


More directives examples

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.


Yet more directives examples

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.


Mips r2000 register usage

MIPS R2000 register usage

  • syntax:$regnumber

    examples:$3, $18, $20

  • all 32 registers are both general purpose, yet have specific uses. . .

    $0always the value 0

    $1used by the assembler for MAL->TAL translation

    $2, $3function return values

    $4 - $7parameters

    $26, $27used by the operating system

    $29stack pointer

    $31return address register

10


Which registers to use

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


Generalities for assembly language

d must designate a register

s1, s2may designate a register or be an immediate

r1, r2must designate a register

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

3


Some arithmetic logical instructions

Some arithmetic/logical instructions


More arithmetic logical instructions

More arithmetic/logical instructions


More arithmetic logical instructions1

More arithmetic/logical instructions


Generalities for assembly language

#C code

#

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

16


Mips load store instructions

MIPS Load/Store Instructions

mnemonic operands operation

lw d, addrone word is loaded from addr and placed into d; addr must be word aligned

lb d, addrone byte is loaded from addr and placed into the rightmost byte of d; sign extension defines the other bits of d

4


More mips load store instructions

more MIPS Load/Store Instructions

mnemonic operands operation

lbud, addrone byte is loaded from addr and placed into the rightmost byte of d; zero extension defines the other bits of d

lid, immedthe immediate value is placed into d

swd, addra word in d is stored to addr; addr must be word aligned

18


More mips load store instructions1

more MIPS Load/Store Instructions

mnemonic operands operation

sbd, addrthe byte in the rightmost byte of d is stored to addr

lad, labelthe address assigned for label is placed into d

19


Generalities for assembly language

loads

l_ $_, addr

stores

s_ $_, addr

addr is one of

  • label

  • ($_)

  • displacement($_)


1 addr is a label

1. addr is a label

memory

X

////////

$8

////////

21

Example:

lw $8, X


2 addr is

2. addr is ($_)

memory

$14

***

////////

***

////////

$10

22

the address is in a register

Example:

lw $10, ($14)


3 addr is displacement

3. addr is displacement($_)

memory

1000

$15

+

1004

////////

////////

$12

23

an address is in a register

Example:

lw $12, 4($15)


Generalities for assembly language

memory

1 byte



X

$20









sign extend

24

Dealing with bytes example:

lb $20, X


Generalities for assembly language

memory

1 byte

////////

Y:

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

Zero Extend

25

More dealing with bytes:

lbu $21, Y


Generalities for assembly language

Example:

sb $13,X

$13 XXXXX

1 byte

XXXXX

X

Only 1 byte of memory changes

memory

26


Generalities for assembly language

memory

Example:

la $22, X

X

X

$22

Much like

int X;

int *pX;

px = &X;

in $22

27


Some branch instructions

Some branch instructions


More branch instructions

More branch instructions


2 more control instructions

2 more control instructions


Generalities for assembly language

Good examples of branch instructions:

beq$8, $9, end_program

bgtz$16, next_check

Incorrect, bad, and wrong:

beqX, $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


Generalities for assembly language

#C code

#

#if ( x == 0 ) {

#y++;

#}

32


Generalities for assembly language

#C code

#

#if ( x == y ) {

#a++;

# y = a – 3;

#}

33


Generalities for assembly language

#C code

#

#sum = 0

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

#sum = sum + i;

#}

34


Generalities for assembly language

#C code

# #define MAX 13

#count = 0;

#while ( count < MAX ) {

#/* code missing! */

#}

35


Generalities for assembly language

#C code

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

#z = z – x;

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

#x--;

#y++;

#}

#}

36


I o instructions

I/O instructions


Generalities for assembly language

#code

getc$21

# execution waits for user input of a

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

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

38


Generalities for assembly language

#code

putc$15

$15 0x52 0x53 0x54 0x55

output

U

^

more output goes here, when there is

more output

39


Generalities for assembly language

.data

str1: .asciiz "hello."

.text

puts str1

hello.

^

more output starts here, when more is printed

40


Generalities for assembly language

.data

str1: .asciiz "hello."

.text

la $12, str1 # address of first

# character in string

puts $12 # address of first

# character to print

# is in $12

hello.

^

more output starts here,

when more is printed

41


Generalities for assembly language

.data

str1: .asciiz "hello.\nMy name is George."

.text

puts str1

hello.

My name is George.

^

more output starts here,

when more is printed

42


Generalities for assembly language

.data

str1: .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


Generalities for assembly language

# MAL program to print out the alphabet

.data

str1: .asciiz "The alphabet:\n"

# register assignments

# $8 -- the ASCII character code

# to be printed

# $9 -- the ASCII code for 'z', the

# ending character

44


Generalities for assembly language

.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, 122

while: bgt $8, $9, all_done

putc $8

add $8, $8, 1

b while

all_done:

li $10, '\n'

putc $10

done

45


Generalities for assembly language

# 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


Generalities for assembly language

.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


  • Login