FASM IRC Bot Prefix - irc

I've been trying to learn how to create an IRC bot in assembler from some old sources. Everything is going fine with my learning except for a prefix problem.
The prefix for the bot is:
CommandPrefix equ "^^"
And the length of the prefix is added with:
add eax, 2d
I want to change the prefix to just "^", but I am having trouble with figuring out what "add eax" should be changed too for it to work. Or even if that is the best way to do it. Any help with this would be appreciated.
Here is what the original code looks like to get some idea:
include "win32ax.inc"
entry Bot
CommandPrefix equ "^^"
section '.code' code readable executable
Bot:
invoke WSAStartup,0101h,WSAData
cmp eax, 0
jne Exit
invoke socket,AF_INET,SOCK_STREAM,0
cmp eax, -1
je Exit
mov dword [SocketDesc], eax
invoke inet_addr,IRCServer
mov dword [SockAddr_IP], eax
invoke htons,IRCPort
mov word [SockAddr_Port], ax
invoke connect,dword [SocketDesc],SockAddr,16d
cmp eax, 0
jne Exit
call GenerateNickname
invoke lstrcpy,SendBuffer,"NICK "
invoke lstrcat,SendBuffer,Nickname
call SendLine
invoke lstrcpy,SendBuffer,"USER "
invoke lstrcat,SendBuffer,Nickname
invoke lstrcat,SendBuffer," 8 * :"
invoke lstrcat,SendBuffer,Nickname
call SendLine
GetMotd:
call RecvLine
call HandlePing
mov ecx, 0
IsMotd:
cmp dword [ReturnBuffer + ecx], "MOTD"
je HaveMotd
cmp byte [ReturnBuffer + ecx], 0d
je GetMotd
inc ecx
jmp IsMotd
HaveMotd:
invoke lstrcpy,SendBuffer,"JOIN "
invoke lstrcat,SendBuffer,Channel
invoke lstrcat,SendBuffer," "
call SendLine
RecvCommand:
call RecvLine
call HandlePing
mov ecx, 0
IsCommand:
cmp word [ReturnBuffer + ecx], CommandPrefix
je HaveCommand
cmp byte [ReturnBuffer + ecx], 0
je RecvCommand
inc ecx
jmp IsCommand
HaveCommand:
mov ebx, ReturnBuffer
add ebx, ecx
add ebx, 2d ;add length of command prefix
invoke lstrcpy,CommandBuffer,ebx
call ExecuteCommand
jmp RecvCommand

Related

How to run an ASM file in VS Code

App: VS Code version 1.65.2
OS: Linux Mint 20.2 Cinnamon
I want to run the following code in Assembly:
; The input is a decimal number,
; Result is a hexadecimal number
section .text
; to make the printf out work the main 'method' is needed
global main
; for printing numbers out
extern printf
main:
; Find factorial for the initial value of ecx,
mov ecx, 5
; Copy the initial value of ecx to eax,
; eax is our factorial result
mov eax, ecx
loop:
; If the counter is less or equal to 1, finish
cmp ecx, 1
jle end
sub ecx, 1
mul ecx
jmp loop
end:
; Display the message and exit
push eax
push message
call printf
add esp, 8
ret
section .data
message: db "The result is = %08X", 10, 0
I have installed:
Cutting edge x86 and x86_64 assembly syntax highlighting.
Arm assembly syntax support for Visual Studio Code.
However, running the file in both extensions returns me the following:
Code language not supported or defined.

x86_64 nasm jumps to the wrong location

I'm working on code to enumerate the PCI bus, but have found that the jz statement for the loop over each device jumps to the wrong location (not even a label). The register function should be getting called for each time cmp ax, 0xffff is inequal, which should be more than once. It is only getting called once.
register: ; eax = edi = config offset of the function
mov dx, ADDR_PRT
add eax, 0x08
in eax, dx
shr eax, 16
mov dx, ax
call checkpoint
mov eax, edi
rmsd: cmp dx, 0x0601 ; mass storage devices
je ahci_register ; register an AHCI controller
ret ; couldn't find it, ignore it
pci_init:
mov edi, 0x80000000
ilp0: mov rax, rdi
mov dx, ADDR_PRT
out dx, eax
mov dx, DATA_PRT
in eax, dx
cmp ax, 0xffff
je ilp0c0
push rdi
mov rax, rdi
call register
pop rdi
ilp0c0: add rdi, 0x100
test edi, 0xff000000 ; code jupms to the line before this
jz ilp0
ret
Code is assembled as a PE file and then linked using lld-link and run using EFI.

MASM x86-64 scanf not reading spaces

I have simple 64 bit assembly program that we are doing for class. It is supposed to take user input (string) and return that string with lowercase letters into uppercase and uppercase into lowercase.
With what I have, it will read anything until it finds a space and this will not read anymore after that. So if I input "test", it will output "TEST". If I input "test Test" it will output "TEST". However, if I add spaces before the first word, it would output the first word but removes the spaces. For example: input " TesT", output: "tESt".
Anyone know how I can go about fixing this?
Here is my whole program:
;Author: Keenan Kaufman
;Date: 10/20/2017
INCLUDELIB msvcrt.lib
printf PROTO
scanf PROTO
exit PROTO
.DATA
CRLF BYTE 0Dh, 0Ah, 0 ;carriage return
msgHeader BYTE "Enter a mixed case string: ", 0
message BYTE 20 DUP(0), 0
target BYTE SIZEOF message DUP(?), 0Dh, 0Ah, 0
msgformat BYTE "%20s", 0
.CODE
main PROC
;Display request for user input
lea rcx, msgHeader
call printf
;obtain user input
lea rcx, msgformat
lea rdx, message
call scanf
lea rsi, message
lea rdi, target
jmp GETNEXT
GETNEXT:
mov al, [rsi]
cmp al, 0
je ENDCASE
cmp al, 'z'
ja NOCHANGE
cmp al, 'A'
jb NOCHANGE
cmp al, 'a'
jae TOUPPER
cmp al, 'Z'
jbe TOLOWER
TOUPPER:
sub al, 32
mov [rdi], al
inc rdi
inc rsi
jmp GETNEXT
TOLOWER:
add al, 32
mov [rdi], al
inc rdi
inc rsi
jmp GETNEXT
NOCHANGE:
mov [rdi], al
inc rdi
inc rsi
jmp GETNEXT
ENDCASE:
jmp FINISH
FINISH:
;Display target
lea rcx, target
call printf
lea rcx, CRLF
call printf
mov rax, 0
call exit
main ENDP
END
Yes, that is a feature of scanf. Here is the Linux man page for scanf which, for the %s format, says:
s
Matches a sequence of non-white-space characters; the next
pointer must be a pointer to the initial element of a character
array that is long enough to hold the input sequence and the
terminating null byte ('\0'), which is added automatically. The
input string stops at white space or at the maximum field width,
whichever occurs first.
To do what you want, read the characters yourself directly from stdin using read(), getc(), or fgets().

C Code NOP confusion

This file is in AT&T syntax - see http://www.imada.sdu.dk/Courses/DM18/Litteratur/IntelnATT.htm
and http://en.wikipedia.org/wiki/X86_assembly_language#Syntax. Both gdb and objdump produce
AT&T syntax by default.
MOV $27163,%ebx
MOV $13156,%eax
MOV $25880,%ecx
CMP %eax,%ebx
JL L1
JMP L2
L1:
IMUL %eax,%ebx
ADD %eax,%ebx
MOV %ebx,%eax
SUB %ecx,%eax
JMP L3
L2:
IMUL %eax,%ebx
SUB %eax,%ebx
MOV %ebx,%eax
ADD %ecx,%eax
L3:
NOP
What is the value of %eax when the last instruction NOP runs?
The answer is "%933%". (no quotes)

Emacs weirdness when trying to comment in Assembly

Suppose I have a block of code like so:
;; outut
mov eax, 4
mov ebx, 1 ; stdout
mov ecx, [ans] ; move biggest element to accumulator
add ecx, 30h ; convert to ascii representation
mov [buff], ecx ; move to memory
mov ecx, buff ; put pointer in ecx for printing
mov edx, 4 ; size, 4 bytes
int 80h ; system call.
When I try to put a comment in the front to comment out a line:
;; outut
;mov eax, 4
mov ebx, 1 ; stdout
mov ecx, [ans] ; move biggest element to accumulator
add ecx, 30h ; convert to ascii representation
mov [buff], ecx ; move to memory
mov ecx, buff ; put pointer in ecx for printing
mov edx, 4 ; size, 4 bytes
int 80h ; system call.
Instead of appearing there where I want it to go, it jumps to here:
;; outut
mov eax, 4 ;
mov ebx, 1 ; stdout
mov ecx, [ans] ; move biggest element to accumulator
add ecx, 30h ; convert to ascii representation
mov [buff], ecx ; move to memory
mov ecx, buff ; put pointer in ecx for printing
mov edx, 4 ; size, 4 bytes
int 80h ; system call.
And no matter what I do, I physically cannot comment out anything.
How can I fix this? It don't remember it always doing this, so i feel like I must have hit some combination of keys and it just happens.
; is bound to asm-comment in assembly mode. You can either do a quoted insert with C-q ; on a case-by-case basis, or remove the binding and just use M-; (comment-dwim) for fancier commenting. If you want to do the latter, set ";" locally to do a self-insert command:
(defun my-hook ()
(local-set-key ";" 'self-insert-command))
(add-hook 'asm-mode-hook 'my-hook)