Thursday, March 26, 2020

.align directive demo code in MIPS

This is a sample MIPS assembler code demonstrating the behaviour of the .align directive in a MIPS assembler program. The code is fully commented.

Launch EzMIPS, copy the following MIPS code and paste it into EzMIPS, the MIPS assembler editor & simulator. Assemble, Run.

# ---------- demonstration of the .align directive in MIPS  -------------- #

# Default section: .text

#.align 4                   # .align directive not allowed in text segment

# ------------------------------------------------------------------------ #

.data

# Default behaviour: 
# .................. 

# Automatic alignment of .half, .word, .float, and .double directives

# ........................................................................ #

#.align -3                  # invalid alignment value <-3>
#.align                     # no alignment value specified


var01: .byte 1, 2, 'Z'      # 0x10010000: 1, 0x10010001: 2, 0x10010002: 0x5a 
str01: .asciiz "Hello\n"    # 0x10010003: Hello\n
var02: .word 0x12345678     # 0x1001000c: 0x12345678
var03: .byte 0x11           # 0x10010010: 0x11

# ........................................................................ #

.align 2                    # align the next variable on the
                            # next word boundary
var04: .half 0xffff         # 0x10010014: 0xffff

# ...................................................................... #

var05: .byte 0xaa           # no alignment specified
                            # 0x10010016: 0xaa
# ...................................................................... #

.align 4                    # align the next variable on the
                            # next 2^4 = 16 boundary

var06: .half 0xbbbb
                            # 0x10010020: 0xbbbb
# ...................................................................... #

.align 0                    # .align 0 turns off automatic alignment of
                            # .half, .word, .float, and .double directives
                            # until the next .data or .kdata directive.

# ...................................................................... #

#                      NO AUTOMATIC ALIGNMENT FROM NOW ON                #

# ...................................................................... #

.align 7                    # align the next variable on the
                            # next 2^7 = 128 boundary

var19: .byte 0xcc           # 0x10010080: 0xcc

# ...................................................................... #

var07: .word 0xffffffff     # We said: NO AUTOMATIC ALIGNMENT
                            # 0x10010081: 0xffffffff

var08: .word 0xaaaaaaaa     # We said: NO AUTOMATIC ALIGNMENT
                            # 0x10010085: 0xaaaaaaaa
# ...................................................................... #

.align 1                    # align the next variable on the
                            # next halfword boundary

var09: .byte 0xbb           # 0x1001008a: 0xbb

# ...................................................................... #

var11: .word 0xdddddddd     # We said: NO AUTOMATIC ALIGNMENT
                            # 0x1001008b: 0xdddddddd

var12: .space 20            # 0x1001008f: -

.align 1
var13: .byte 0x99           # align the next variable on the
                            # next halfword boundary
                            # 0x100100a4: 0x99
# ---------------------------------------------------------------------- #
.text

main:
    #.align 2               # .align directive not allowed in text segment
    sll $zero, $zero, 0

# ---------------------------------------------------------------------- #

.data

# Default behaviour: 
# .................. 

# Automatic alignment of .half, .word, .float, and .double directives

# This is a NEW .data secttion so: AUTOMATIC ALIGNMENT from noe on
# ie forget the NO AUTOMATIC ALIGNMENT of the previous .data section!
# ...................................................................... #

var14: .word 0xeeeeeeee     # 0x100100a8: 0xeeeeeeee

# ---------------------------------------------------------------------- #

Please let me know of any suggestions or bugs regarding the code above.

Regards,

Antonis

No comments:

Post a Comment