These routines can be used to convert between number and ASCII string formats. The SWI calls illustrated here are: OS_ConvertHex8.
OS_ConvertHex8 can be used to convert an hexadecimal number into its equivalent ASCII string. The call is easy to set up.
On entry:
R0 The value to be converted
R1 The memory address where the ASCII string will be placed.
R2 The length of the string buffer
On exit:
R0 Points to the string buffer
R1 Points to the terminating zero byte in the buffer
R2 Holds the number of free bytes in the string buffer.
This is the program which is converting 1024, in this example. Change line 140 to suit, or edit file to read a value in from the keyboard.
10 REM >PROGRAMxtra01
20 REM RPI ARM Assembler HOG
30 REM Convert ‘Number to hex string’
40 REM using OS_ConvertHex8
50 REM www.brucesmith.info
60 :
70 DIM code%(256)
80 DIM buffer(32)
90 FOR pass=0 TO 3 STEP 3
100 P%=code%
110 [
120 OPT pass
130 .convertHex8
140 MOV R0, #1024 ; number to be converted
150 ADR R1, buffer ; address of buffer for string
160 MOV R2, #32 ; length of buffer
170 SWI “OS_ConvertHex8”
180 SWI “OS_Write0”
190 MOV R15,R14
200 ]
210 NEXT pass
220 PRINT “Numeric string is : “;
230 CALL convertHex8
The ‘8’ in the SWI call can be changed to ‘1’, ‘2’, “4”, or ‘6’ to change the number of digits translated into a string. The routine adds 0’s to the left side of the number to ensure the specified number of digits are returned.
OS_ConvertBinary4 can be used to convert an hexadecimal number into its equivalent binary ASCII string. The call is easy to set up.
On entry:
R0 The value to be converted
R1 The memory address where the ASCII string will be placed.
R2 The length of the string buffer
On exit:
R0 Points to the string buffer
R1 Points to the terminating zero byte in the buffer
R2 Holds the number of free bytes in the string buffer.
This is the program which is converting 1000, in this example. Change line 140 to suit, or edit file to read a value in from the keyboard.
10 REM >ProgramXtra2
20 REM RPi ARM Assembler HOG
30 REM Convert Number to binary string
40 REM using OS_ConvertBinary4
50 REM www.brucesmith.info
60 :
70 DIM code% (256)
80 DIM buffer (64)
90 FOR pass=0 TO 3 STEP 3
100 P%=code%
110 [
120 OPT pass
130 .convertBinary4
140 MOV R0, #1000 ; number to be converted
150 ADR R1, buffer ; address of buffer for string
160 MOV R2, #64 ; length of buffer
170 SWI “OS_ConvertBinary4”
180 SWI “OS_Write0”
190 MOV R15,R14
200 ]
210 NEXT pass
220 PRINT “Binary string is : “;
230 CALL convertBinary4
The ‘4’ in the SWI call can be changed to ‘1’, ‘2’, or ‘3’. The number tells the routine how many bytes in R0 are to be converted. The routine adds 0’s to the left side of the number to ensure the specified number of digits are returned.
SWI Conversion Routines
Calculated Conversion
This routine takes a single byte ASCII string, which is assumed to be an hexadecimal value, and coverts it into its equivalent internal binary value.
The ‘readchars’ subroutine (lines 280-
If a character in the range 0 to 9 is entered all that needs to be done is for the high nibble of the characters ASCII byte to be masked out because the low nibble binary is the same as the hex character itself (line 450). The test in line 430 is seeing if the value in R0 is less than ASC”;” which is just after ‘9’ in the ASCII code table.
Converting characters A to F is a little less obvious. However, if the high nibble is again masked off, then the value remaining is nine less than the hex value required. We effectively ‘add’ this in the subtraction of line 480 (this masks and adds nine at the same time!).
The two bytes are combined in line 230. Note that as we use a couple of BL commands to call subroutines, we must save our ASCII return address on entry (line 150) so we can return safely (line 250).
10 REM >ProgramXtra4
20 REM RPi Assembly Language HOG
30 REM ASCII Hex to Binary conversion
40 :
50 DIM code% 256
60 hichar=2
70 lowchar =1
80 temp=3
90 keeplink=4
100 FOR pass = 0 TO 3 STEP 3
110 P%= code%
120 [
130 OPT pass
140 .start
150 MOV keeplink, R14 ; save LR for BASIC return
160 BL readchars
170 MOV R0, hichar
180 BL check
190 MOV R0, R0, ASL #4 ; move into high nibble
200 MOV temp, R0
210 MOV R0, lowchar
220 BL check
230 ORR temp, temp, R0 ; combine nibbles
240 MOV R0, temp
250 MOV R14, keeplink
260 MOV PC, LR
270 ;
280 .readchars
290 SWI “OS_WriteS”
300 EQUS “Type the two hex number to be converted: &”
310 EQUB 0
320 ALIGN
330 SWI “OS_ReadC”
340 SWI 0
350 MOV hichar, R0
360 SWI “OS_ReadC”
370 SWI 0
380 MOV lowchar, R0 ; low
390 SWI “OS_NewLine”
400 MOV PC,R14
410 ;
420 .check
430 CMP R0, #58 ; is it ’9’ or less?
440 BCS atof ; if not branch over
450 AND R0,R0, #15 ; yes, so save low nibble
460 MOV PC,R14
470 .atof
480 SBC R0, R0, #55 ; mask nibble and subtract 9
490 MOV PC,R14
500 ]
510 NEXT
520 result=USR (start)
530 PRINT “Result is: ” result
File Operation
This routine uses the SWI OS_BGet call to read a file. This program requests a file name and then will count the number of spaces in it. It uses this to return the number of words in the file, if we assume one space between each word. It is an approximation at least, but is useful to illustrate how file can be opened and read. On entry, R1 should contain the file handle and on return, R0 will contain the byte read. The Carry flag ‘ C is clear if the byte was read correctly and set if a problem arose (most likely through an end-
10 REM >ProgramXtra3
20 REM RPi Assembly Language HOG
30 REM Example of OS_BGet to count
40 REM spaces/words in a file
50 REM www.brucesmith.info
60 :
70 DIM WordCount 1024
80 FOR pass = 0 TO 3 STEP 3
90 P%= WordCount
100 [
110 OPT pass
120 \ On entry R0 points to name of file
130 MOV R10,#0 \ total of number of spaces
140 MOV R1,R0 \ Points to file name
150 MOV R0,#64 \ Open for read access only
160 MOV R2,#0
170 SWI “OS_Find” \ Returns file handle in R0
180 MOV R8,R0
190 .countloop
200 MOV R1,R8
210 SWI “OS_BGet” \ Get byte from file
220 BCS quitloop \ If EOF quit loop
230 CMP R0,#32 \ Is it a space ?
240 ADDEQ R10,R10,#1 \ If so, increment the count
250 BAL countloop \ If not end of file then loop
260 .quitloop
270 MOV R1,R8
280 MOV R0,#0
290 SWI “OS_Find” \ Close file
300 MOV R0,R10 \ Make word count available to USR
310 MOVS PC,R14
320 ]
330 NEXT
340 MODE 0
350 DIM filename 16
360 INPUT “Please enter the filename to be investigated : ” $filename
370 PRINT “Counting words now”
380 A% = filename
390 words=USR (WordCount)
400 PRINT “Number of words counter is: ” words