Given the following code:
digraph q2a {
rankdir=LR;
s [label="s"];
0 [label="0"];
00 [label="00"];
001 [label="001"];
0010 [label="0010"];
00100 [label="00100"];
001000 [label="001000"];
0010001 [label="0010001",shape=doublecircle];
s -> s [label="1"];
s -> 0 [label="0"]
0 -> 00 [label="0"];
0 -> s [label="1"];
{ rank=same; 001 -> 00 [label="1",dir="back"] }
00:s -> 00:s [label="0"];
{ rank=same; 0010 -> 001 [label="0"] }
001 -> s [label="1"];
0010 -> 00100 [label="0"];
0010 -> s [label="1"];
{ rank=same; 00100 -> 001000 [label="0"] }
00100 -> 001 [label="1"];
001000 -> 0010001 [label="1"];
001000 -> 00 [label="0"];
0010001 -> 0010001 [label="0,1"];
}
I get the following graph:
Despite the fact that I set "001 -> 00 [label="1",dir="back"], the edge from 001 to 00 is still pointing forward.
If I remove the first line ("rankdir=LR"), I get the following graph:
Now the edge from 001 -> 00 is correctly pointing back.
Is there any explanation for this behavior?
Same graph, different output here - this particular problem may be linked to your version of graphviz.
Using graphviz 2.31.20130312.0445, the direction of the links are correctly displayed, event with rankdir=LR.
Related
I am trying to implement MD5 in Ruby, following the pseudo code written in the wiki.
Here is the codes, not working well:
# : All variables are unsigned 32 bit and wrap modulo 2^32 when calculating
# s specifies the per-round shift amounts
s = []
s.concat([7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22])
s.concat([5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20])
s.concat([4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23])
s.concat([6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21])
# Use binary integer part of the sines of integers (Radians) as constants:
k = 0.upto(63).map do |i|
(Math.sin(i+1).abs * 2 ** 32).floor
end
# Initialize variables:
a0 = 0x67452301 # A
b0 = 0xefcdab89 # B
c0 = 0x98badcfe # C
d0 = 0x10325476 # D
message = File.read(ARGV[0])
# Pre-processing
# with bit stream "string" (MSB)
bits = message.unpack('B*')[0]
org_len = bits.size
bits << '1' # adding a single 1 bit
bits << '0' while !((bits.size + 64) % 512 == 0) # padding with zeros
bits << (org_len % 2 ** 64).to_s(2).rjust(64, '0')
message32 = [bits].pack('B*').unpack('V*') # ?
# 1. bits.scan(/(.{8})/).flatten.map { |b| b.to_i(2).to_s(16).rjust(2, '0') }.each_slice(16) { |c| puts c.join(' ')} => for test
# 2. [bits].pack('B*').unpack('N*') == bits.scan(/(.{32})/).flatten.map { |b| b.to_i(2).to_s(16).rjust(8, '0').to_i(16) } => true
# custom operations for wrapping the results as modulo 2 ** 32
class Integer
def r
self & 0xFFFFFFFF
end
def rotate_l(count)
(self << count).r | (self >> (32 - count))
end
end
# Process the message in successive 512-bit chunks:
message32.each_slice(16).each do |m|
a = a0
b = b0
c = c0
d = d0
0.upto(63) do |i|
if i < 16
f = d ^ (b & (c ^ d))
g = i
elsif i < 32
f = c ^ (d & (b ^ c))
g = (5 * i + 1) % 16
elsif i < 48
f = b ^ c ^ d
g = (3 * i + 5) % 16
elsif i < 64
f = c ^ (b | ~d)
g = (7 * i) % 16
end
f = (f + a + k[i] + m[g]).r
a = d
d = c
c = b
b = (b + f.rotate_l(s[i])).r
end
a0 = (a0 + a).r
b0 = (b0 + b).r
c0 = (c0 + c).r
d0 = (d0 + d).r
end
puts [a0, b0, c0, d0].pack('V*').unpack('H*')
I'm testing with messages, well known for collision in just one block:
Message 1
Message 2
They are resulted in the same value, but not correct:
❯ ruby md5.rb message1.bin
816922b82e2f8d5bd3abf90777ad72c9
❯ ruby md5.rb message2.bin
816922b82e2f8d5bd3abf90777ad72c9
❯ md5 message*
MD5 (/Users/hansuk/Downloads/message1.bin) = 008ee33a9d58b51cfeb425b0959121c9
MD5 (/Users/hansuk/Downloads/message2.bin) = 008ee33a9d58b51cfeb425b0959121c9
I have an uncertainty about pre-processing steps.
I checked the bit stream after pre-processing, with the comments on the line 34 and 35, the original message written in same and the padding bits are right:
❯ hexdump message1.bin
0000000 4d c9 68 ff 0e e3 5c 20 95 72 d4 77 7b 72 15 87
0000010 d3 6f a7 b2 1b dc 56 b7 4a 3d c0 78 3e 7b 95 18
0000020 af bf a2 00 a8 28 4b f3 6e 8e 4b 55 b3 5f 42 75
0000030 93 d8 49 67 6d a0 d1 55 5d 83 60 fb 5f 07 fe a2
0000040
(byebug) bits.scan(/(.{8})/).flatten.map { |b| b.to_i(2).to_s(16).rjust(2, '0') }.each_slice(16) { |c| puts c.join(' ')}
4d c9 68 ff 0e e3 5c 20 95 72 d4 77 7b 72 15 87
d3 6f a7 b2 1b dc 56 b7 4a 3d c0 78 3e 7b 95 18
af bf a2 00 a8 28 4b f3 6e 8e 4b 55 b3 5f 42 75
93 d8 49 67 6d a0 d1 55 5d 83 60 fb 5f 07 fe a2
80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 00
nil
What am I missed?
The one most classical mistake in implementing MD5 is botching endianness: the padded message and the message length are to be turned to 32-bit words per little-endian convention, so that message 'abc' in ASCII (0x61 0x62 0x63) is turned to a 16-word padded message block with m[0]=0x80636261, m[14]=0x18, and m[1…13,15]=0.
I never wrote anything in Ruby, but I get the feeling the code yields m[0]=0x61626380, m[15]=0x18, and m[1…14]=0.
Also: the 4-word result is to be turned to 16 bytes per little-endian convention too.
I have written this code:
digraph G {
A254 -> A10[style=invis];
A10 -> A9[style=invis];
A9 -> A8[style=invis];
A8 -> A7[style=invis];
A7 -> A6[style=invis];
A6 -> A5[style=invis];
A5 -> A4[style=invis];
A4 -> A3[style=invis];
A3 -> A2[style=invis];
A2 -> A1[style=invis];
A254 -> A8 [label="t"];
A8 -> A10 [label="t", style=dotted];
A8 -> A9 [label="t", style=dotted];
A8 -> A7 [label="t", style=dotted];
A8 -> A6 [label="t", style=dotted];
A8 -> A3 [constraint = false, label="t"];
A3 -> A5 [label="t", style=dotted];
A3 -> A4 [label="t", style=dotted];
A3 -> A2 [label="t", style=dotted];
A3 -> A1 [label="t", style=dotted];
A254[style=filled]
A3[style=filled]
A8[style=filled]
{rank=same; A254,A10,A9,A8,A7,A6,A5,A4,A3,A2,A1}
}
It produces the following graph:
Actually I have three questions:
1.How can I make the edge A8 -> A3 neater? It looks very bad.
2.How can I make the edges A254 -> A8 + A8 -> A3 rectangular?
3.How can I make this graph vertical?
EDIT:
It is essential that the nodes are lined up and in the same order shown in the graph above.
This
is generated from
digraph G {
A254 -> A10[style=invis];
A10 -> A9[style=invis];
A9 -> A8[style=invis];
A8 -> A7[style=invis];
A7 -> A6[style=invis];
A6 -> A5[style=invis];
A5 -> A4[style=invis];
A4 -> A3[style=invis];
A3 -> A2[style=invis];
A2 -> A1[style=invis];
A254 -> A8 [label="t"];
A8 -> A10 [label="t", style=dotted];
A8 -> A9 [label="t", style=dotted];
A8 -> A7 [label="t", style=dotted];
A8 -> A6 [label="t", style=dotted];
A8 -> A3 [label="t"];
A3 -> A5 [label="t", style=dotted];
A3 -> A4 [label="t", style=dotted];
A3 -> A2 [label="t", style=dotted];
A3 -> A1 [label="t", style=dotted];
A254[style=filled]
A3[style=filled]
A8[style=filled]
{rank=same; A254,A8,A3}
}
The best thing to do is read the documentation and experiment with the ideas in there
I have found the solution, here it is
You can make the edge neater by making a suitable adjustments to the distance between nodes and to the size of the nodes. And the most important thing is to force the A8->A3 edge to be longer by setting the minlen attribute of the corrosponding edge.
The following code
digraph G {
nodesep=0.5
node[fixedsize=true, shape="circle", width=0.5]
A254 -> A10[style=invis];
A10 -> A9[style=invis];
A9 -> A8[style=invis];
A8 -> A7[style=invis];
A7 -> A6[style=invis];
A6 -> A5[style=invis];
A5 -> A4[style=invis];
A4 -> A3[style=invis];
A3 -> A2[style=invis];
A2 -> A1[style=invis];
A254 -> A8 [label="t"];
A8 -> A10 [label="t", style=dotted];
A8 -> A9 [label="t", style=dotted];
A8 -> A7 [label="t", style=dotted];
A8 -> A6 [label="t", style=dotted];
A8 -> A3 [minlen = 3, constraint = false, label="t"];
A3 -> A5 [label="t", style=dotted];
A3 -> A4 [label="t", style=dotted];
A3 -> A2 [label="t", style=dotted];
A3 -> A1 [label="t", style=dotted];
A254[style=filled]
A3[style=filled]
A8[style=filled]
{rank=same; A254,A10,A9,A8,A7,A6,A5,A4,A3,A2,A1}
}
generates the following graph:
Edges can not be rectangular.
You can make the graph vertical by using the command rotate=90
#include <stdio.h>
int main()
{
int alpha,numeric;
for(alpha='A';alpha<'K';alpha++)
{
for(numeric=0;numeric<10;numeric++)
{
printf("%c%d\t",alpha,numeric);
}
}
return 0;
}
The output I got is as follows.
A0 A1 A2 A3 A4 A5 A6 A7 A8 A9
B0 B1 B2 B3 B4 B5 B6 B7 B8 B9
C0 C1 C2 C3 C4 C5 C6 C7 C8 C9
D0 D1 D2 D3 D4 D5 D6 D7 D8 D9
E0 E1 E2 E3 E4 E5 E6 E7 E8 E9
F0 F1 F2 F3 F4 F5 F6 F7 F8 F9
G0 G1 G2 G3 G4 G5 G6 G7 G8 G9
H0 H1 H2 H3 H4 H5 H6 H7 H8 H9
I0 I1 I2 I3 I4 I5 I6 I7 I8 I9
J0 J1 J2 J3 J4 J5 J6 J7 J8 J9
But, more neat and in form of a matrix, without any space between lines.
Someone please tell me how the printing is entering to new line even though there is no newline(\n) character in the program.
I'm attempting to write a short LC-3 program that initializes R1=5, R2=16 and computes the sum of R1 and R2 and put the result in memory x4000. The program is supposed to start at x3000. Unfortunately, I have to write it in binary form.
This is what I have so far...
.orig x3000__________; Program starts at x3000
0101 001 001 1 00000 ;R1 <- R1 AND x0000
0001 001 001 1 00101 ;R1 <- R1 + x0005
0101 010 010 1 00000 ;R2 <- R2 AND x0000
0001 010 010 1 01000 ;R2 <- R2 + x0008
0001 010 010 1 01000 ;R2 <- R2 + x0008
0001 011 010 0 00 001 ;R3 <- R2 + R1
//This last step is where I'm struggling...
I was thinking of using ST, and I figured PCOFFSET9 to be 994, but I can't represent that using 8 bits... so how else would I do this? Is my code inefficient?
0011 011
The ST command is limited to only 511 (I believe) from its current location in memory. For something like this you will need to use the STI command (Store Indirect) The sample code below will help explain how to use STI.
.orig x3000
AND R1, R1, #0 ; Clear R1
ADD R1, R1, #5 ; Store 5 into R1
AND R2, R2, #0 ; Clear R2
ADD R2, R2, #8 ; Store 8 into R2
ADD R3, R2, R1 ; R3 = R2 + R1
STI R3, STORE_x4000 ; Store the value of R3 into mem[x4000]
HALT ; TRAP x25 end the program
; Variables
STORE_x4000 .FILL x4000
.END
You will need to make the appropriate conversions to binary, but if you plug the code into the LC-3 simulator it will give you the binary representation.
I initially (asked for help) and wrote a BASIC program in the 6502 pet emulator which added two n-byte integers. However, my feedback was that it was simply adding two 16 bit integers (not adding n-byte integers).
Can anyone help me understand this feedback by looking at my code and point me in the right direction to make a program that adds two n-byte integers?
Thank You for the collaboration!
Documentation:
Adds two n-byte integers using absolute indexed addressing. The addends begin at memory locations $0600, $0700 and the answer is at $0800. Byte length of the integers is at $0600 (¢ —> 256)
Machine Code:
18 a2 00 ac 00 06 bd 00
07 7d 00 08 9d 00 09 e8
00 88 00 d0
Op Codes, Documentation, Variables:
A1 = $0600
B1 = $0700
B2 = $0800
Z1 = $0900
[START] = $0500
CLC 18 // loads x with 0
LDX A2 00 // loads length on Y
LDY A1 AC 00 06 // load first operand
loop: LDA B1, x BD 00 07 // adds second operand
ADC B2, x 7D 00 08 // store result
STA Z1, x 9D 00 09 // go to next byte
INX E8 00 // count how many are left
DEY 88 00 // do more if needed
BNE loop D0
It looked to me like your code does what you claim -- adds two N byte operands in little-endian byte order. I vaguely remembered the various addressing modes of the 6502 from my misspent youth and the code seems fine. X is used to index the current byte from the two numbers, Y is a counter for the length of the operands in bytes and you loop over those bytes, stored at addresses 0x0700 and 0x0800 and write the result at address 0x0900.
Rather than get the Commodore 64 out of the attic and try it out I used an online virtual 6502 simulator. On this site we can set the memory address and load the byte values in. They even link to a page to assemble opcodes too. So setting the memory locations 0x0600 to "04" and both 0x0700 and 0x0800 to "04 03 02 01" we should see this code add these two 32 bit values (0x01020304 + 0x01020304 == 0x02040608).
Stepping through the code by clicking on the PC register and setting it to 0x0500 and then single stepping we see there is a bug in your machine code. After INX which compiles to E8 we hit a spurious 0x00 value(BRK) which terminates. The corrected code as below runs to completion and the expected value is seen by reading the memory at 0x0900.
0000 CLC 18
0001 LDX #$00 A2 00
0003 LDY $0600 AC 00 06
0006 LOOP: LDA $0700,X BD 00 07
0009 ADC $0800,X 7D 00 08
000C STA $0900,X 9D 00 09
000F INX E8
0010 DEY 88
0011 BNE LOOP: D0 F3
Memory dump:
:0900 08 06 04 02 00 00 00 00
:0908 00 00 00 00 00 00 00 00