|
| 1 | +use std::process::exit; |
| 2 | + |
| 3 | +pub const R_FLAG: &str = "-r"; |
| 4 | +pub const H_FLAG: &str = "-h"; |
| 5 | +pub const HELP: &str = " |
| 6 | + *******************Booth's Algorithm Step Visualization*************************** |
| 7 | + - by Jyotismoy Kalita |
| 8 | + |
| 9 | + ------------------------------------ HELP ---------------------------------------- |
| 10 | + FLAGS: |
| 11 | + -r X: Specify the register bit size where X is the specified size. 0 < X <= 16. |
| 12 | + If Register flag is not used when executing the program. The option to specify register will |
| 13 | + show during runtime. |
| 14 | +
|
| 15 | + -h: Use help. |
| 16 | +
|
| 17 | + DEFINITION OF STEPS: |
| 18 | + Initialization: Initial Value after loading up Multiplier and Multiplicand. |
| 19 | + A <- A + M: Values of Register A & M are added and put back into A register. |
| 20 | + A <- A - M: Values of Register M subtracted from A and put into A register. |
| 21 | + ASR: Arithmetic Shift Right. The MSB is retained while all other bits are shifted right starting from A to Q-1. |
| 22 | +
|
| 23 | + ALGORITHM: |
| 24 | + Action is determined by the Bits stored in 0th bit of Q and -1th bit of Q a.k.a Q-1. Here's the table for the actions - |
| 25 | +
|
| 26 | + Q Q-1 Action |
| 27 | + -------------------------------- |
| 28 | + 0 0 ASR |
| 29 | + 0 1 A <- A + M then ASR |
| 30 | + 1 0 A <- A - M then ASR |
| 31 | + 1 1 ASR |
| 32 | + -------------------------------- |
| 33 | + This Algorithm is repeated as many times as there are Bits in the Registers. |
| 34 | + E.g. If Register size if 4, the Algorithm is repeated 4 times. |
| 35 | +
|
| 36 | +"; |
| 37 | + |
| 38 | +pub fn initialize_size(args: Vec<String>) -> usize { |
| 39 | + let size: usize; |
| 40 | + if args.contains(&H_FLAG.to_string()) { |
| 41 | + if args.contains(&R_FLAG.to_string()) { |
| 42 | + println!("Note: Cannot run program while in Help mode\n"); |
| 43 | + } |
| 44 | + println!("{}", HELP); |
| 45 | + exit(0); |
| 46 | + } |
| 47 | + if args.contains(&R_FLAG.to_string()) { |
| 48 | + let pos = args.iter().position(|x| x == R_FLAG).unwrap() + 1; |
| 49 | + let n = args |
| 50 | + .get(pos) |
| 51 | + .expect("Register Flag used but size not specified\nUse -h for help."); |
| 52 | + size = n |
| 53 | + .parse::<usize>() |
| 54 | + .expect("Invalid Register Size specified. Use -h for help.\n"); |
| 55 | + } else { |
| 56 | + size = crate::booth::get_size(); |
| 57 | + } |
| 58 | + if size == 0 { |
| 59 | + println!("Register Size specified cannot be 0. Use -h for help."); |
| 60 | + exit(1); |
| 61 | + } |
| 62 | + if size > 16 { |
| 63 | + println!("Register Size specified exceeds maximum limit of 16. Use -h for help."); |
| 64 | + exit(2); |
| 65 | + } |
| 66 | + println!( |
| 67 | + "Input Range: {} to {}", |
| 68 | + -2_i32.pow(size as u32 - 1), |
| 69 | + (2_i32.pow(size as u32 - 1) - 1) |
| 70 | + ); |
| 71 | + println!( |
| 72 | + "Output Range: {} to {}\n", |
| 73 | + -2_i32.pow(size as u32 * 2 - 1), |
| 74 | + (2_i32.pow(size as u32 * 2 - 1) - 1) |
| 75 | + ); |
| 76 | + size |
| 77 | +} |
0 commit comments