Wednesday, 4 January 2017

A PowerPC instruction a day series 2

:  first POWER5 program (1)
Listing 1. Your first POWER5 program
#Data sections holds writable memory declarations
.data
.align 3  #align to 8-byte boundary

#This is where we will load our first value from
first_value:
        #"quad" actually emits 8-byte entities
        .quad 1
second_value:
        .quad 2

#Write the "official procedure descriptor" in its own section
.section ".opd","aw"
.align 3 #align to 8-byte boundary

#procedure description for ._start
.global _start
#Note that the description is named _start,
# and the beginning of the code is labeled ._start
_start:
        .quad ._start, .TOC.@tocbase, 0

#Switch to ".text" section for program code
.text
._start:
        #Use register 7 to load in an address
        #64-bit addresses must be loaded in 16-bit pieces

        #Load in the high-order pieces of the address
        lis 7, first_value@highest
        ori   7, 7, first_value@higher
        #Shift these up to the high-order bits
        rldicr 7, 7, 32, 31
        #Load in the low-order pieces of the address
        oris 7, 7, first_value@h
        ori  7, 7, first_value@l

        #Load in first value to register 4, from the address we just loaded
        ld 4, 0(7)

        #Load in the address of the second value
        lis 7, second_value@highest
        ori 7, 7, second_value@higher
        rldicr 7, 7, 32, 31
        oris 7, 7, second_value@h
        ori 7, 7, second_value@l

        #Load in the second value to register 5, from the address we just loaded
        ld 5, 0(7)

        #Calculate the value and store into register 6
        add 6, 4, 5

        #Exit with the status
        li 0, 1    #system call is in register 0
        mr 3, 6    #Move result into register 3 for the system call

        sc


Object and executable files are divided up into "sections". Each section is loaded into a different place in the address space when the program is executed. They have different protections and purposes. The main sections we will concern ourselves with are:
.data
contains the pre-initialized data used for the program
.text
contains the actual code (historically known as the program text)
.opd
contains "official procedure declarations" which are used to assist in linking functions and specifying the entry point to a program (the entry point is the first instruction in the code to be executed)


 
The first thing our program does is switch to the .data section, and set alignment to an 8-byte boundary (.align 3 advances the assembler's internal address counter until it is a multiple of 2^3).

contains "official procedure declarations" which are used to assist in linking functions and specifying the entry point to a program (the entry point is the first instruction in the code to be executed)

The first thing our program does is switch to the .data section, and set alignment to an 8-byte boundary (.align 3 advances the assembler's internal address counter until it is a multiple of 2^3).
The line that says first_value: is a symbol declaration. This creates a symbol called first_value which is synonymous with the address of the next declaration or instruction listed in the assembler. Note that first_value itself is a constant, not a variable, though the memory address it refers to may be updateable. first_value is simply an easy way to refer to a specific address in memory.

A PowerPC instruction a day series: 1

Notice that all instructions that compute a value use the first operand as the destination register. In all of these instructions the registers are specified only by their number. For example, the instruction for loading the number 12 into register 5 is li 5, 12. We know that 5 refers to a register and 12 refers to the number 12 because of the instruction format -- there is no other indicator.
Each PowerPC instruction is 32 bits long. The first six bits determine the instruction and the remaining portions have different functions depending on the instruction. The fact that they are fixed-length allows the processor to process them more efficiently. However, the limitation to 32 bits can cause a few headaches, which we will encounter. The solutions to most of these headaches will be discussed in part 2.
Many of the instructions above make use of the PowerPC extended mnemonics. This means that they are actually specializations of a more general instruction. For example, all of the conditional branches mentioned above are actually specializations of the bc (branch conditional) instruction. The form of the bc instruction is bc MODE, CBIT, ADDRESS. The CBIT is the bit of the condition register to test. The MODE has lots of interesting uses, but for simple uses you set it to 12 if you want to branch if the condition bit is set, and 4 if you want to branch if the condition bit is not set. Some of the important condition register bits are 8 for less than, 9 for greater than, and 10 for equal. Therefore, the instruction beq ADDRESS is really bc 12, 10 ADDRESS. Likewise li is a specialized form of addi and mr is a specialized form of or. These extended mnemonics help make PowerPC assembly language programs more readable and writable for simpler programs, while not removing the power available for more advanced programs and programmers.

Tuesday, 3 January 2017

Shell Script 15 Regular Expressions - User Guide (c)

POSIX Character Class Definitions

POSIX 1003.2 section 2.8.3.2 (6) defines a set of character classes that denote certain common ranges. They tend to look very ugly but have the advantage that also take into account the 'locale', that is, any variant of the local language/coding system. Many utilities/languages provide short-hand ways of invoking these classes. Strictly the names used and hence their contents reference the LC_CTYPE POSIX definition (1003.2 section 2.5.2.1).

Value

Meaning

[:upper:]Any alpha character A to Z.
[:lower:]Any alpha character a to z.
[:digit:]Only the digits 0 to 9
[:blank:]Space, TAB characters only.
[:xdigit:]Hexadecimal notation 0-9, A-F, a-f.
[:punct:]Punctuation symbols . , " ' ? ! ; : # $ % & ( ) * + - / < > = @ [ ] \ ^ _ { } | ~
[:cntrl:]Control Characters NL CR LF TAB VT FF NUL SOH STX EXT EOT ENQ ACK SO SI DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC IS1 IS2 IS3 IS4 DEL.
[:space:]Any whitespace characters (space, tab, NL, FF, VT, CR). Many system abbreviate as \s.
[:alnum:]Any alphanumeric character 0 to 9 OR A to Z or a to z (the set defined by upper, lower and digit)
[:alpha:]Any alpha character A to Z or a to z (the set defined by upper and lower).
[:print:]Any printable character (set defined by alnum and punct) plus the single character SPACE.
[:graph:]Any printable characters (set defined by alnum and punct) but excludes the single character SPACE. Many system abbreviate as \W.
These are always used inside square brackets in the form [[:alnum:]] or combined as [[:digit:]a-d]

Monday, 2 January 2017

Shell Script 14 Regular Expressions - User Guide (b)

Metacharacter

Meaning

?The ? (question mark) matches when the preceding character occurs 0 or 1 times only, for example, colou?r will find both color (u is found 0 times) and colour (u is found 1 time).
*The * (asterisk or star) matches when the preceding character occurs 0 or more times, for example, tre* will find tree (e is found 2 times) and tread (e is found 1 time) and trough (e is found 0 times and thus returns a match only on the tr).
+The + (plus) matches when the preceding character occurs 1 or more times, for example, tre+ will find tree (e is found 2 times) and tread (e is found 1 time) but NOT trough (0 times).
{n}Matches when the preceding character, or character range, occurs n times exactly, for example, to find a local phone number we could use [0-9]{3}-[0-9]{4} which would find any number of the form 123-4567. Value is enclosed in braces (curly brackets).Note: The - (dash) in this case, because it is outside the square brackets, is a literal. Louise Rains writes to say that it is invalid to commence a NXX code (the 123) with a zero (which would be permitted in the expression above). In this case the expression [1-9][0-9]{2}-[0-9]{4} would be necessary to find a valid local phone number.
{n,m}Matches when the preceding character occurs at least n times but not more than m times, for example, ba{2,3}b will find baab and baaab but NOT bab or baaaab. Values are enclosed in braces (curly brackets).
{n,}Matches when the preceding character occurs at least n times, for example, ba{2,}b will find 'baab', 'baaab' or 'baaaab' but NOT 'bab'. Values are enclosed in braces (curly brackets).

Examples:
            (1)
jtony@genoa:~/learn/shell/UserGuide$ grep -rn  --color 'W*in' string1.txt
1:Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)
2:Mozilla/4.75 [en](X11;U;Linux2.2.16-22 i586)
jtony@genoa:~/learn/shell/UserGuide$ grep -rn  --color 'W*in' string2.txt
1:Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)
2:Mozilla/4.75 [en](X11;U;Linux2.2.16-22 i586)

(2):
egrep -rn --color '[xX][0-9a-z]{2}'  string2.txt
2:Mozilla/4.75 [en](X11;U;Linux2.2.16-22 i586)
 


Sunday, 1 January 2017

Shell Script 13 Regular Expressions - User Guide (a)

Metacharacter

Meaning


^The ^ (circumflex or caret) inside square brackets negates the expression (we will see an alternate use for the circumflex/caret outside square brackets later), for example, [^Ff] means anything except upper or lower case F and [^a-z] means everything except lower case a to z.
Notes:
  1. There are no spaces between the range delimiter values, if there was, depending on the range, it would be added to the possible range or rejected as invalid. Be very careful with spaces.
  2. Some regular expression systems, notably VBScript, provide a negation operator (!) for use with strings. This is a non-standard feature and therefore the resulting expressions are not portable.
  3. Because of the dual nature of the caret (or circumflex) you will frequency see expressions like [^"], [^<] or [^,] which are typically used as separator triggers (when combined with iterations) for more more complex searches or when parsing, say, HTML or comma delimited text.
e.g.

jtony@genoa:~/learn/shell/UserGuide$ cat string1.txt
Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)
Mozilla/4.75 [en](X11;U;Linux2.2.16-22 i586)
jtony@genoa:~/learn/shell/UserGuide$ cat string2.txt
Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)
Mozilla/4.75 [en](X11;U;Linux2.2.16-22 i586)

jtony@genoa:~/learn/shell/UserGuide$ grep -rn  '[^A-M]in'  string1.txt
1:Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)

jtony@genoa:~/learn/shell/UserGuide$ grep -rn  '[A-Za-z]in'  string1.txt
1:Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)
2:Mozilla/4.75 [en](X11;U;Linux2.2.16-22 i586)








This series refer the online article:
http://www.zytrax.com/tech/web/regex.htm#search

Shell Script 13 : Regular Expression (c)

You can also for matches that appear at the beginning or end of words, not whole lines.
jtony@genoa:~/learn/shell$ cat test
specialize
aaaaaaaaaa
tomcat
tom
atom
tom333
jyjtom
specialise
jtony@genoa:~/learn/shell$ grep '\<tom' test
tomcat
tom
tom333
jtony@genoa:~/learn/shell$ grep 'tom\>' test
tom
atom
jyjtom

How to make grep highlight keyword with colors?

add option --color
e.g.
jtony@genoa:~/learn/shell/UserGuide$ grep -rn --color 'in[du]'  string2.txt
1:Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)
2:Mozilla/4.75 [en](X11;U;Linux2.2.16-22 i586)