Literals/String: Difference between revisions

Line 1,732:
<lang ML/I>This is the first mention of Alice
and here we mention Bob again</lang>
 
=={{header|MIPS Assembly}}==
{{works with|https://github.com/Kingcom/armips ARMIPS}}
Strings are specified using single or double quotes. The assembler will convert each letter of the string into its ASCII equivalent during the assembly process. Therefore, all of the following statements have the same effect:
 
<lang mips>li a0,'A'
li a0,0x41
li a0,65
li a0,0b01000001</lang>
 
This means that you can do compile-time "character addition/subtraction" and the like, to better communicate <i>why</i> your code is doing what it's doing.
<lang mips>;print 0 if $t0 if even, 1 if $t0 is odd
 
andi t0,t0,1 ;clear all but bit 1. This tells us if $t0 is odd or even.
addiu t0,"0" ;add ASCII 0 (0x30) to $t0
jal PrintChar ;implementation-defined print routine that prints the ASCII value of $t0 to the screen.</lang>
 
ASCII strings use <code>.byte</code> for declaration. Control codes are implemented by strategically placing commas and their numeric values <i>outside of quotation marks,</i> like so:
 
<lang mips>MyString:
.byte "Hello World!",13,10,0 ;carriage return, line feed, null terminator
.align 4 ;pads to the next 4 byte-boundary</lang>
 
As with most RISC CPUs, alignment is a must, especially when working with ASCII strings. ARMIPS doesn't provide alignment automatically, but it does have the <code>.align</code> directive to provide sufficient padding (if necessary) to ensure everything after your string is properly aligned. If it was already aligned, the directive will do nothing rather than burn the bytes, meaning that you don't have to take the time to count how long your string is. There's no memory wasted by dropping a <code>.align</code> after every piece of byte-length data, so might as well.
 
 
=={{header|Modula-3}}==
1,489

edits