Skip to content

Instantly share code, notes, and snippets.

@maxchehab
Created September 27, 2018 02:37
Show Gist options
  • Save maxchehab/2e0fd1948b44d89902b250cda9855387 to your computer and use it in GitHub Desktop.
Save maxchehab/2e0fd1948b44d89902b250cda9855387 to your computer and use it in GitHub Desktop.
;**********************************************************************************************
; Author: Maxwell Chehab
; Pre Lab 1: Data Conversions and Signed Numbers
; Date Created: September 20, 2018
; Last Modified:
; Description: this program will compute Convert A BCD number to xten and xunit place. Convert
; them into a binary number and perform a simple calculation
; Inputs: none
; Outputs: view the specified locations (0x20000000 to 0x20000005)
;**********************************************************************************************
THUMB
AREA MyData, DATA, READWRITE, ALIGN=2
X RN R5
N RN R7
AREA MyCode, CODE, READONLY, ALIGN=2
EXPORT __main
__main
BL BCD_to_bin ; Go to ConvertBCDToBIN
BL Absolute_value ; Go to ConvertBCDToBIN
MOV R0, #0xFF
Absolute_value
PUSH { LR, R1 }
;MOV N, #0xFFFFFFBB ; Assign -69 to N
MOV N, #0x00000045 ; Assign 69 to N
MOV R1, N
CMP R1, #0
BGE positive
BL negative
POP { LR, R1 }
BX LR
positive
MOV R8, N
BX LR
negative
NEG R8, R1
BX LR
BCD_to_bin
PUSH { R1 - R3 }
MOV X, #0x26 ; Store x as decided value (0x26)
MOV R1, X ; Store BCD X number in R1
MOV R2, R1 ; Store BCD X number in R2
AND R1, #0x0000000F ; R1 has the units digits
LSR R2, #4 ; R2 has the tens digit
MOV R3, R2 ; R3 gets a copy of R2
LSL R2, #1 ; R2 gets 2*Xten
ADD R2, R3, LSL #3 ; R2 gets 2 * Xten + 8 * Xten = 10Xten
ADD R2, R1 ; R2 gets 10 * Xten + Xunit
MOV R6, R2 ; R5 now has the binary number
POP { R1 - R3 }
BX LR
;LDR R0, =Xten ; find the location of Xten
;LDR R0, =Xunit ; find the location of Xunit
;LDR R0, =Xten_ascii ; find the location of Xten_ascii
;LDR R0, =Xunit_ascii ; find the location of Xunit_ascii
;LDR R0, =Xbin ; find the location of Xbin
;LDR R0, =Ybin ; find the location of Ybin
; Step 1 Split BCD Number
;MOV R1, X ; R1 gets a copy of X
;MOV R2, R1 ; R2 gets a copy of X
;AND R1, X, #0x0000000F ; Xunit has the units digit
;LDR R0, =Xunit ; R0 <- address of Xunit
;STRB R1, [R0] ; Store R1 in Xunit
;LSR R2, #4 ; R2 has the tens digit
;LDR R0, =Xten ; R0 <- address of Xten
;STRB R2, [R0] ; Store R2 in Xten
; Step 2 Convert to Ascii
;LDR R0, =Xunit ; Store Xunit address in R0
;LDR R1, [R0] ; Store value at R0 into R1
;ADD R1, #48 ; Add 48 to R1
;LDR R0, =Xunit_ascii ; R0 <- address of Xunit_ascii7
;STRB R1, [R0] ; Store R1 in Xunit_ascii
;LDR R0, =Xten ; Store Xten address in R0
;LDR R1, [R0] ; Store value at R0 into R1
;ADD R1, #48 ; Add 48 to R1
;LDR R0, =Xten_ascii ; R0 <- address of Xten_ascii
;STRB R1, [R0] ; Store R1 in Xten_ascii
; Step 3 Convert to single binary number
;MOV X, #0x26 ; Store x as decided value (0x26)
;MOV R1, X ; Store BCD X number in R1
;MOV R2, R1 ; Store BCD X number in R2
;AND R1, #0x0000000F ; R1 has the units digits
;LSR R2, #4 ; R2 has the tens digit
;MOV R3, R2 ; R3 gets a copy of R2
;LSL R2, #1 ; R2 gets 2*Xten
;ADD R2, R3, LSL #3 ; R2 gets 2 * Xten + 8 * Xten = 10Xten
;ADD R2, R1 ; R2 gets 10 * Xten + Xunit
;LDR R0, =Xbin ; R0 <- address of Xbin
;STRB R2, [R0] ; Store R2 in Xbin
; Step 4 Ybin = 50 - 3 * Xbin
;MOV R5, R2 ; Store value at R2 into R5
;MOV R1, R5 ; Make a copy of Xbin in R1
;LSL R5, R5, #1 ; Xbin * 2
;ADD R5, R5, R1 ; Xbin * 2 + Xbin
;MOV R2, #50 ; R2 = 50
;SUB R2, R5 ; 50 - R5
;LDR R0, =Ybin ; R0 <- address of Ybin
;STRB R2, [R0] ; Store R2 in Ybin
;LSL R5, R5, #3 ; R5= 8*R5=8*Vx
;SUB R5, R5, Vx ; R5= R5-Vx=8*Vx=7*Vx
;ADD R5, R5, #120 ; R5= R5+120=(7*Vx)+120
;LDR R0, =Vy ; R0= address of Vy
;STR R5, [R0] ; Vy= R5
; Vz = Vy / 8 + 25
;LSR R5, R5, #3 ; R5= 8/R5=8/Vy
;ADD R5, R5, #25 ; R5 = R5/8+25
;LDR R0, =Vz ; R0= address of Vz
;STR R5, [R0] ; Vz= R5
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment