---
# System prepended metadata

title: National Sun Yat-Sen University　ASSEMBLY LANGUAGE AND MICROCOMPUTER　Homework \#3 Tested on 12/09/2021
tags: [assembly language, CSE310, NSYSU]

---

---
title: National Sun Yat-Sen University　ASSEMBLY LANGUAGE AND MICROCOMPUTER　Homework \#3 Tested on 12/09/2021
tags: assembly language,CSE310,NSYSU 
---

![](https://i.imgur.com/29WgDpB.png)

Answer:

```
init_Indices: mov r3, #0 // load i = 0
loop:         mov r4, #0 // load 0 into r4
              cmp r3, r2 // compare i and s
              strlt r4, [r1, r3, LSL, #2] // if i < s, then a[i] = 0
              addlt r3, r3, #1 // if i < s, i += 1
              blt loop // if i < s, still loop
```

![](https://i.imgur.com/w9U9aLR.png)
![](https://i.imgur.com/tTbc54n.png)

Answer:

Hello, arm-gcc
Good Morning: 0x34333231

* arm default is little endian.

![](https://i.imgur.com/PHDRefl.png)

Answer:

.algin 2 only effects for the next instruction.
For the red mark .align 2, the previos instruction is .short 0x1122, if you don't put the red .align 2, then the byte 0x31, 0x32 will be put in the remain 2 bytes for the privious word.

![](https://i.imgur.com/UP9xlOY.png)

```ldr r1, Label1 + 8```: r1 = 0x34333231 (little endian)
```ldr r1, =Label1 + 8```: r1 = the address of .byte 0x31, 0x32, 0x33, 0x34

![](https://i.imgur.com/PzsEWIp.png)

Answer:

#### (a) SUBLSS r3, r2, r1, LSR, \#7
```
| cond | 00 | # | opcode | S |  Rn  |  Rd  |    operand2     |
| 1001 | 00 | 0 |  0010  | 1 | 0010 | 0011 | 00111 01 0 0001 | (immediate type 1)
```

#### (b) STRB r5, [r1, r0, LSL \#2]
```
| cond | 01 | # | P | U | B | W | L |  Rn  |  Rd  |     offset      |
| 1110 | 01 | 1 | 1 | 1 | 1 | 0 | 0 | 0001 | 0101 | 00010 00 0 0000 | (register shift type)
```

#### (c\) STREQ r4, [r2, #-3]!
```
| cond | 01 | # | P | U | B | W | L |  Rn  |  Rd  |     offset      |
| 0000 | 01 | 0 | 1 | 0 | 0 | 1 | 0 | 0010 | 0100 | 0000 0000 0011  |
```

#### (d) LDMED r13!, {r0-r2, r14}
```
| cond | 100 | P | U | S | W | L |  Rn  |   register list  |
| 1110 | 100 | 1 | 1 | 0 | 1 | 1 | 1101 | 0100 0000 0000 0111 |
```

#### (e) MULNE r1, r2, r3
```
| cond | 0000 | mul | S | Rd/RdHi | Rn/RdLo |  Rs  | 1001 |  Rm  |
| 0001 | 0000 | 000 | 0 |   0001  |  0000   | 0011 | 1001 | 0010 |
```

#### (f) LDRH r3, [r2], #6
```
| cond | 000 | P | U | # | W | L |  Rn  |  Rd  | offsetH | 1 | S | H | 1 | offsetL
| 1110 | 000 | 0 | 1 | 1 | 0 | 1 | 0010 | 0011 |  0000   | 1 | 0 | 1 | 1 |   0110
(post index is automatically write back.)
```

#### (g) LDMEA sp!, {r3, r9, r1}
```
| cond | 100 | P | U | S | W | L |  Rn  |   register list  |
| 1110 | 100 | 1 | 0 | 0 | 1 | 1 | 1101 | 0000 0010 0000 1010 |
```

![](https://i.imgur.com/vcYdMIw.png)
![](https://i.imgur.com/duMY4t0.png)

Answer:

Use these 3 instruction to make a example. The exception **IRQ** takes place when **execution**, so the pc_value (usually is about to the **IF**) will be **108**. After finishing IRQ handling, arm should continue dealing with the next instruction after the instruction caused IRQ exception, will be **104**, in this instance, that why you should substract 4 from the value in lr_<IRQ>(which is also the pc_value under the user mode) before you put it back into user mode's pc.
    
![](https://i.imgur.com/biEQ1ff.png)

Answer:

MOVS pc, r14. *(Use for the ruturn of **SWI** and **Undef instruction** instruction)*

![](https://i.imgur.com/qTIigWk.png)

Answer:

```
mrs r0, CPSR
bic r0, #0x80000000
msr CPSR_f, r0
```
    
![](https://i.imgur.com/v3s5fuP.png)

Answer:
```
/* suppose a in r0, b in r1, c in r2, d in r3, 
e in a stack of memory(the r13-stack pointer points to)* /
    
sum: stmfd r13!, {r4, r5, fp, lr}
     // r4 is used to save the result
     add r4, r0, r1
     add r4, r4, r2
     sub r4, r4, r3
     /* r5 is used to put e, we should try to load e out from 
     memory. */
     ldr r5, [sp]
     sub r4, r4, r5, LSL #1
     // when ready to exit subrotinue, restore the saved 
     register value and put result in r0, r1.
     mov r0, r4
     ldmfd r13!, {r4, r5, fp, lr}
     mov r15, r14
```

![](https://i.imgur.com/x6X7yvw.png)

    
Answer:

```
// suppose the chracter sent into the function is in r0

// only chracter between 'a' - 'z' will be dealt with
    
transfer: stmfd sp!, {fp, lr}
          cmp r0, #'a'
          blt exit
          cmp r0, #'z'
          suble r0, #32
exit:     ldmfd sp!, {fp, lr}
          mov pc, lr
    
```