Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using branch instructions Write an ARM program to read and convert a 32-bit 2\'s

ID: 3726163 • Letter: U

Question

using branch instructions

Write an ARM program to read and convert a 32-bit 2's complement into a signed decimal string in the data area, Declare and initialize a null-terminated string with a label of HexStr and an initial value of your choice (each byte is the ASCII of a Hex symbol in this 32-bit 2's complement number). For example, the string could be "A8F", 0 declare and initialize a signed word as 0, label this word as TwosComp reserved a chunk of zeroed memory with a length of 12 bytes, label this chunk of memory as DecStr (why a length of 12 bytes is enough?) reserved a chunk of zeroed memory with a length of 11 bytes, label this chunk of memory as RvsDecStr (why a length of 11 bytes is enough?) o Store each one of the above four labels as a word with an address label, e.g., "adrHexStr DCD HexStr for HexStr, and EXPORT each address label, e.g., "EXPORT adrHexStr" in the main program, read the ASCII's of up to eight symbols of a Hex number (e.g., 1A2B3D4D or FFF4B3FA) symbol by symbol from the memory at HexStr, and convert this list of symbols in ASCII into a 32-bit two's complement number. You may assume that this Hex number will be zero-extended if less than eight symbols are read, i.e., the missing leading symbols are zeros (e.g., A79 means 00000A79) store this 32-bit 2's complement number at TwosComp convert the 32-bit signed number at TwosComp to a NULL terminated ASCII string, each byte is the SCII of the minus sign or a digit in this number. A minus sign, ,needs to be placed at the beginning if this number is negative. E. g., if[TwosComp] = 0x1 A2B3C4D, then it should be converted into 439041 101"; if[TwosComp-0xFFF4B3FA, then it should be converted into "-740358" Hint: when you keep dividing the absolute value of this number by 10, the list of remainders that you obtain are the digits whose ASCIIs are going to be stored at DecStr. However, the first . remainder is the Least Significant Digit and the last remainder is the Most Significant Digit! . Therefore, you might want to store the ASClIls of the digits you obtained through division to RvsDecStr first, then reversely store them to the memory at DecStr. write a your own subroutine to divide a positive 32-bit number by 10 and put the quotient and the remainder in two registers, respectively, as the output parameters. call this subroutine for division-by-10 while converting the signed number into its decimal equivalent. You are NOT allowed to call the division subroutine that is provided by the emulator Store this decimal string as a NULL-terminated string to memory labeled as DecStr.

Explanation / Answer

Using 32bit 2's complement

DIM org 200

REM Register assignments

bin = 0

sgn = 1

ptr = 3

ch = 4

sp = 13

link = 14

cr = &0D

FOR pass=0 TO 2 STEP 2

P%=org

[ opt pass

.testAscToBin

;Test routine for ascToBin

;

STMFD (sp)!,{link} ;Save return address

ADR ptr,digits ;Set up pointer to the string

BL ascToBin ;Convert it to binary in R0

LDMFD (sp)!,{PC} ;Return with result

;

.digits

EQUS "-123456"

EQUB cr

;

;ascToBin. Read a string of ASCII digits at ptr,

;optionally preceded by a + or - sign. Return the

;signed binary number corresponding to this in bin.

;

.ascToBin

STMFD (sp)!,{sgn,ch,link}

MOV bin,#0 ;Init result

MOV sgn,#0 ;Init sign to pos.

LDRB ch,[ptr,#0] ;Get possible + or -

CMP ch,#ASC"+" ;If +,just skip

BEQ ascSkp

CMP ch,#ASC"-" ;If -,negate sign and skip

MVNEQ sgn,#0

.ascSkp

ADDEQ ptr,ptr,#1 ;Inc ptr if + or -

.ascLp

LDRB ch,[ptr,#0] ;Read digit

SUB ch,ch,#ASC"0" ;Convert to binary

CMP ch,#9 ;Make sure it is a digit

BHI ascEnd ;If not,finish

ADD bin,bin,bin ;Get bin*10. bin=bin*2

ADD bin,bin,bin,ASL #2 ;bin=bin*5

ADD bin,bin,ch ;Add in this digit

ADD ptr,ptr,#1 ;Next character

B ascLp

.ascEnd

TEQ sgn,#0 ;If there was - sign

RSBMI bin,bin,#0 ;Negate the result

LDMFD (sp)!,{sgn,ch,pc}

]

NEXT pass

PRINT "These should print the same:"

PRINT $digits ' ;USRtestAscToBin