70 likes | 237 Views
Character Strings. Programs need to manipulate more than numbers (integers) Character encoding EBCDIC ( E xtended B inary- C oded D ecimal I nterchange C ode ) ASCII ( A merican S tandard C ode for I nformation I nterchange ) 7bit -> 8bit (ISO-8859)
E N D
Character Strings • Programs need to manipulate more than numbers (integers) • Character encoding • EBCDIC (Extended Binary-Coded Decimal Interchange Code) • ASCII (American Standard Code for Information Interchange) • 7bit -> 8bit (ISO-8859) • UNICODE (8/16/24bit variable width) Ch.3 - Machine Instructions - III
ASCII Encoding Ch.3 - Machine Instructions - III
ISO-Latin-1 Encoding (high bit) Ch.3 - Machine Instructions - III
Sequences of Characters (Strings) Three ways to encode: • Store length at head of string • Store length in accompanying variable (inside format structure) • Termination character at end of sequence Ch.3 - Machine Instructions - III
Sequences of Characters (Strings) Termination character at end of sequence • C/C++ convention (code “0” for terminator) Ch.3 - Machine Instructions - III
C void strcpy (char x[], char y[]) { int i = 0; while (y[i] != NULL) { x[i] = y[i]; i = i + 1; } x[i] = NULL; } MIPS strcpy: addi $sp,$sp,-4 sw $s0,0($sp) addi $s0,$zero,0 L1: add $t1,$a1,$s0 lb $t2,0($t1) add $t3,$a0,$s0 sb $t2,0($t3) addi $s0,$s0,1 bne $t2,$zero,L1 lw $s0,0($sp) addi $sp,$sp,4 jr $ra Manipulating Strings Ch.3 - Machine Instructions - III
Manipulating Strings (con’t) strcpy: addi $sp,$sp,-4 # Allocate stack frame sw $s0,0($sp) # Store current $s0 addi $s0,$zero,0 # $s0 (i) initialized to 0 L1: add $t1,$a1,$s0 # $t1 gets address of y[i] lb $t2,0($a1) # $t2 gets byte at y[i] add $t3,$a0,$s0 # $t3 gets address of x[i] sb $t2,0($t3) # $t2 value stored addi $s0,$s0,1 # i++ bne $t2,$zero,L1 # Loop back if char in $t2 # is non-null lw $s0,0($sp) # restore $s0 addi $sp,$sp,4 # release stack frame jr $ra # return to caller Ch.3 - Machine Instructions - III