Vous êtes sur la page 1sur 5

Homework #3 Due: Sept.

30 9 am Reading: Chapter 7, 8

Answer the following questions. When code is requested, include actual .s file.
State whether this code has been assembled and run successfully.
1. Write an assembly language program that calls a subroutine which converts all
lower case letters of a string to upper case. Memory location of the string is passed
on the top of the stack. (40)
;***************************************************************
; Leon
; Date
; HW3_Prob 1
; SampleCode.s
; Description of the program SampleCode
;***************************************************************

;***************************************************************
; EQU Directives
; These directives do not allocate memory
;***************************************************************
;SYMBOL DIRECTIVE VALUE COMMENT

SYM_ZERO EQU 0x00


SYM_SRAM EQU 0x20000000 ; Beginning
of SRAM memory
Stack EQU 0x00000400 ; Stack size
output EQU 0x20000900 ; Output
String

;***************************************************************
; Data Section in READWRITE
; Values of the data in this section are not properly initialazed,
; but labels can be used in the program to change the data values.
;***************************************************************
;LABEL DIRECTIVE VALUE COMMENT

AREA |.sdata|, DATA, READWRITE


THUMB
SDATA1 DCB 0x00 ; SDATA1 is
at 0x20000000 memory address
SDATA2 DCB "A short string!" ; This string
will not be initialize as it is in R/W

;***************************************************************
; Stack Area
;***************************************************************
;SYMBOL DIRECTIVE VALUE COMMENT

AREA STACK, NOINIT, READWRITE, ALIGN=3


StackMem SPACE Stack

;***************************************************************
; Reset Area
;***************************************************************
;LABEL DIRECTIVE VALUE COMMENT

AREA RESET, CODE, READONLY


THUMB
EXPORT __Vectors
__Vectors
DCD StackMem + Stack ; Top of
Stack
DCD Reset_Handler ; Reset
Handler

;***************************************************************
; Directives - This Data Section is part of the code
; It is in the read only section so values cannot be changed.
;***************************************************************
;LABEL DIRECTIVE VALUE COMMENT
AREA |.data|, DATA, READONLY
THUMB
MSG1 DCB "ECE251 is cool!"
DCB 0x0D ; New line
DCB 0x04 ; End of
message.

DATA1 DCB 0x00 ; Useful data

;***************************************************************
; Program section
;***************************************************************
;LABEL DIRECTIVE VALUE COMMENT

AREA |.text|, CODE, READONLY


THUMB
EXTERN OutStr
EXPORT Reset_Handler ; Make
Reset_Handler available to linker
Reset_Handler

start
LDR R0,=output
LDR R1,=MSG1
readchar LDRB R2,[R1] ;Read in
one char in message
STR
R2,[R1] ;Write this char to memory we
want make change
ADD
R0,#1 ;Increse Address
ADD R1,#1
CMP
R2,#0x04 ;Check the end of the String
BNE readchar
LDR R5,=output
BL
OutStr ;Print out Original String
LDR R0,=output
;Reload the register to R0
PUSH {R0} ;Pass
String Address to Stack
BL convert
LDR R5,=output ;Load
Address after converting
BL
OutStr ;Print out the result

Finis B Finis ; stops your


program if your code doesn't
;*****************************
convert
POP {R0} ;Get
Address of String
Continue LDRB R1,[R0] ;Get one byte
of the target string
CMP R1,#0x61 ; See if the
byte belongs to lowercase letter
BLT Donothing
CMP R1,#0x7a
BGT Donothing
SUB R1,#0x20 ;Doint
conversion
STRB R1,[R0] ;Store back
to memory
Donothing ADD R0,#1 ;Increment
CMP
R1,#0x04 ;Check end of the string
BNE Continue
BX LR
;Back to main
;*****************************
; End of the program section
;***************************************************************
;LABEL DIRECTIVE VALUE COMMENT

END

2. Write an assembly language subroutine that checks whether a number (passed


in R0) is a prime number or not. (40)
;***************************************************************
; Author: Yi Wang
;***************************************************************
; EQU Directives
; These directives do not allocate memory
;***************************************************************
;LABEL DIRECTIVE VALUE COMMENT
;***************************************************************
; Directives - This Data Section is part of the code
; It is in the read only section so values cannot be changed.
;***************************************************************
;LABEL DIRECTIVE VALUE COMMENT
AREA sdata, DATA, READONLY
THUMB
MSG1 DCB "YES"
DCB 0x0D
DCB 0x04
MSG2 DCB "NO"
DCB 0x0D
DCB 0x04
;***************************************************************
; Program section
;***************************************************************
;LABEL DIRECTIVE VALUE COMMENT
AREA main, READONLY, CODE
THUMB
EXTERN OutChar
EXPORT __main
;***************************************************************
; Start of the main program section
;***************************************************************
__main
start MOV r0, #8; the number being tested
LDR r1, =0x20000400
STR r0, [r1]
LDR r1, [r1]
MOV r2, #w
BL compare
down B down

compare PUSH {LR}


CMP r1, r2
BEQ show1
UDIV r3, r0, r2
MUL r4, r3, r2
SUB r5, r1, r4
CMP r5, #0
BEQ show2
ADD r2, #1
B compare
show1 LDR r5, =MSG1
BL OutStr
POP {LR}
BX LR
show2 LDR r5, =MSG2
BL OutStr
POP {LR}
BX LR
;***************************************************************
; End of the program section
;***************************************************************
;LABEL DIRECTIVE VALUE COMMENT
ALIGN
END

3. Compared with iterative methods, what are the advantages and disadvantages of
recursive methods? (20)
Advantages: Natural way to solve problems, easy to debug, more conceptual.
Disadvantages: High time and memory overhead and also hard to implement
in some case.

Vous aimerez peut-être aussi