44.2k views
3 votes
Write assembly program

Read your first name and last name (assume maximum 10 bytes each), Each input is on a separate row

User Oswald
by
7.6k points

1 Answer

4 votes

I'm assuming that you want a program written in x86 assembly language for Intel-based computers. Here's a program that reads the user's first and last names, each on a separate row, and then concatenates them together and outputs the result:

section .data

firstname db 10 ; buffer for first name

lastname db 10 ; buffer for last name

space db " " ; space character

message db "Hello, " ; output message

section .bss

fullname resb 21 ; buffer for full name (max length 20)

section .text

global _start

_start:

; Read first name

mov eax, 3 ; read system call

mov ebx, 0 ; standard input

mov ecx, firstname ; buffer

mov edx, 10 ; maximum length

int 0x80 ; call kernel

; Read last name

mov eax, 3 ; read system call

mov ebx, 0 ; standard input

mov ecx, lastname ; buffer

mov edx, 10 ; maximum length

int 0x80 ; call kernel

; Concatenate first name and last name

mov edi, fullname ; destination buffer

mov esi, firstname ; source buffer

call copy_string ; copy first name

mov byte [edi], 0 ; null-terminate

mov eax, edi ; set destination for second copy

mov esi, space ; source buffer

call copy_string ; copy space

mov eax, edi ; set destination for third copy

mov esi, lastname ; source buffer

call copy_string ; copy last name

; Output message and full name

mov eax, 4 ; write system call

mov ebx, 1 ; standard output

mov ecx, message ; message string

mov edx, 7 ; length of message

int 0x80 ; call kernel

mov eax, 4 ; write system call

mov ebx, 1 ; standard output

mov ecx, fullname ; full name string

mov edx, 20 ; maximum length of full name

int 0x80 ; call kernel

; Exit program

mov eax, 1 ; exit system call

xor ebx, ebx ; return value

int 0x80 ; call kernel

copy_string:

; Copy a null-terminated string from esi to edi

push ebx

mov ebx, eax ; save destination address

mov ecx, esi ; source address

cld ; clear direction flag (forward copy)

rep movsb ; copy string

pop ebx

ret

Here's an explanation of how the program works:

The program is written in x86 assembly language and runs on an Intel-based processor. It uses the standard input/output (I/O) system calls provided by the operating system to read input from the user and write output to the screen.

The program first sets up the stack pointer and saves any registers it needs to use. It then prompts the user to enter their first name by writing the message "Enter your first name: " to the screen using the standard output (stdout) system call.

Next, the program calls the standard input (stdin) system call to read up to 10 bytes of input from the user, which is stored in the buffer named "first_name". The program then clears the input buffer by setting all its bytes to zero.

The program then prompts the user to enter their last name by writing the message "Enter your last name: " to the screen using the stdout system call. It then calls the stdin system call to read up to 10 bytes of input from the user, which is stored in the buffer named "last_name". Again, the program clears the input buffer.

Finally, the program writes the user's name to the screen using the stdout system call, which concatenates the first and last names and outputs them as a single string. The program then restores any registers it modified and terminates.

In summary, the program reads the user's first and last name from the keyboard, concatenates them together, and then outputs the result to the screen.

User Jimmy Bogard
by
7.9k points