简体   繁体   中英

Asm Console Input/Output

I am trying to make a basic 'game' (input/output) in the console with assembly. As I am new to assembly, I am not too sure of how things work in lower level languages: I am used to the C++ / C# environment.

What I am trying to achieve is that the user types in their name, and the console outputs whether it is the same as the variable myName or not. If I have made mistakes in my code I would be very grateful if you could correct them

.text
.global main

main:
myName db "Name"
call readline 
cmp myName, ebx
je same
jne notsame

same:
mov eax, "We have the same name"
jmp print

notsame:
mov eax, "We have different names"
jmp print

readline:
; read console line
mov ebx, line ; line is whatever the input is
ret

print:
; do something with eax

EDIT: Would this be any better?

.text
.global main

main:
myName db "Name"
call readline 
pop ebx
cmp myName, ebx
je same
jne notsame

same:
push "We have the same name"
jmp print

notsame:
push "We have different names"
jmp print

readline:
; read console line
push line ; line is whatever the input is
ret

print:
pop eax
; do something with eax

Is this homework?

The code is all kinds of off. First, console input is not a scalar - it's a text string. That is, an arbitrary set of characters. You can't expect it to fit into a single register. You need to declare a buffer and pass it to the console reading routine so that the characters are placed there. Second, to compare strings, you need to do that character by character - strings are not integers. So, a loop would be necessary. Read up on those.

Third, you really need to tell everyone what's your operating system. The specifics of console I/O would depend on that. On Linux, it's syscalls. On Windows, it's Win32 API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM