BHI Programming Three Different Codes Exercises
ANSWER
- Python Command Line Program:
import sys
if len(sys.argv) != 4:
print("Usage: python myProgram.py param1 param2 param3")
else:
param1, param2, param3 = sys.argv[1], sys.argv[2], sys.argv[3]
print("Parameter 1:", param1)
print("Parameter 2:", param2)
print("Parameter 3:", param3)
To run this program, save it as “myProgram.py” and execute it with three command-line parameters like this:
python myProgram.py page1.htm page2.htm page3.htm
- Description of Registers and Stacks in Intel Assembly Language:
Registers in Intel Assembly Language: Registers are small, fast storage locations within the CPU that are used to store data temporarily during program execution. The x86 architecture includes several general-purpose registers (like EAX, EBX, ECX, EDX) and special-purpose registers (like EIP for instruction pointer). Registers are used for various operations, such as arithmetic, logic, and data manipulation.
Stacks in Intel Assembly Language: A stack is a region of memory used for storing data in a last-in-first-out (LIFO) manner. The stack is often used for function call management, saving and restoring register values, and managing local variables. The stack pointer (ESP/RSP) and base pointer (EBP/RBP) registers are used to manage the stack.
Sample Assembly Language Code: Here’s a simple assembly code snippet to illustrate the operation of registers and stacks:
section .data
message db "Hello, Assembly!",0
length equ $ - message
section .text
global _start
_start:
; Load message address into ESI
mov esi, message
; Print the message using syscalls
mov eax, 4 ; syscall number for sys_write
mov ebx, 1 ; file descriptor 1 (stdout)
mov ecx, esi ; pointer to the message
mov edx, length ; length of the message
int 0x80 ; interrupt to invoke syscall
; Exit the program
mov eax, 1 ; syscall number for sys_exit
xor ebx, ebx ; exit code 0
int 0x80 ; interrupt to invoke syscall
This assembly code prints “Hello, Assembly!” to the console using the sys_write syscall and then exits the program using the sys_exit syscall.
- Python Program for User Authentication:
# Hard-coded username and password
correct_username = "user123"
correct_password = "strongPassword123"
# Get user input
username = input("Enter your username: ")
password = input("Enter your password: ")
# Check username and password
if username == correct_username and password == correct_password:
print("Authentication successful!")
else:
print("Authentication failed.")
if len(password) < 8:
print("Password is too short. It should be at least 8 characters long.")
This Python program prompts the user for a username and password, checks them against the hard-coded values, and warns the user if the password is considered weak (less than 8 characters long).
Screenshots: Here are sample screenshots of the program in action:
Enter your username: user123
Enter your password: strongPassword123
Authentication successful!
Enter your username: user123
Enter your password: weak
Authentication failed.
Password is too short. It should be at least 8 characters long.
QUESTION
Description
1. Write a program in pure Python, C, C++ or C# console to accept 3 run time parameters from the command line and display the parameters back to the user from the code.For example python myProgram page1.htm page2.htm page3.htm
2. With reference to Intel assembly language, describe how registers and stacks operate Include sample assembly language code to show the operation of each.Please add the Description and Assembler Example.
3.Write a program to ask a user for a username and password. Check the user name and password against hard coded values. Validate the inputs and warn the user if the password is considered weak. Provide the code and sreen captures proving your program works. Please use a language that we have used in class such as c# or Python.