this is my Assembly source code for windows socket (ws2)
this program compiled without any problem but on execution time, program crash (for seconds, program do nothing)
what is the problem ? i think the problem is about stack or ...
format PE CONSOLE
entry start
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
section '.bss' writeable readable
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
____STD_OUTPUT_HANDLE RD 1
____SOCKET RD 1
____LPWSADATA RB 400
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
section '.text' code readable executable
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
start:
sub esp, 16 ; struct sockaddr_in
; GetStdHandle
push -11
call [GetStdHandle]
test eax, eax
jz .exit
mov [____STD_OUTPUT_HANDLE], eax
; WSAStartup
push ____LPWSADATA
push 2
call [WSAStartup]
test eax, eax
jnz .init_failed
; SOCKET
push 6
push 1
push 2
call [socket]
test eax, eax
jz .socket_failed
; HTONS (PORT)
push 80
call [htons]
mov [esp+2], WORD ax ; sin_port = htons(port)
; SOCKADDR
mov [esp], WORD 2 ; sin_family = AF_INET
mov [esp+4], DWORD 2130706433 ; sin_addr = 127.0.0.1 (addr in long = (2130706433))
; BIND
push 16
lea eax, [esp]
push eax
push [____SOCKET]
call [bind]
test eax, eax
jnz .bind_failed
; LISTEN
push 5 ; SOMAXCONN
push [____SOCKET]
call [listen]
test eax, eax
jnz .listen_failed
; CLOSE
; ...
; ...
jmp .exit
.init_failed:
push 0
push 0
push DWORD [.err0_len]
push DWORD .err0
push [____STD_OUTPUT_HANDLE]
call [WriteFile]
jmp .exit
.socket_failed:
push 0
push 0
push DWORD [.err1_len]
push DWORD .err1
push [____STD_OUTPUT_HANDLE]
call [WriteFile]
jmp .exit
.bind_failed:
push 0
push 0
push DWORD [.err2_len]
push DWORD .err2
push [____STD_OUTPUT_HANDLE]
call [WriteFile]
jmp .exit
.listen_failed:
push 0
push 0
push DWORD [.err3_len]
push DWORD .err3
push [____STD_OUTPUT_HANDLE]
call [WriteFile]
jmp .exit
.exit:
push 0
call [ExitProcess]
hlt
.err0:
DB 'Socket initialization failed', 0x0A, 0x00
.err0_len:
DD $ - .err0
.err1:
DB 'Socket creation failed', 0x0A, 0x00
.err1_len:
DD $ - .err1
.err2:
DB 'Socket bind failed', 0x0A, 0x00
.err2_len:
DD $ - .err2
.err3:
DB 'Socket listen failed', 0x0A, 0x00
.err3_len:
DD $ - .err3
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
section '.idata' import data readable writeable
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DD 0,0,0,RVA kernel_name, RVA kernel_list
DD 0,0,0,0,0
DD 0,0,0,RVA ws2_name, RVA ws2_list
DD 0,0,0,0,0
kernel_list:
ExitProcess DD RVA _ExitProcess
GetStdHandle DD RVA _GetStdHandle
WriteFile DD RVA _WriteFile
CloseHandle DD RVA _CloseHandle
DD 0
kernel_name: DB 'KERNEL32.DLL', 0
_ExitProcess: DB 0,0,'ExitProcess',0
_GetStdHandle: DB 0,0,'GetStdHandle',0
_WriteFile: DB 0,0,'WriteFile',0
_CloseHandle: DB 0,0,'CloseHandle',0
ws2_list:
socket DD RVA _socket
htons DD RVA _htons
WSAStartup DD RVA _WSAStartup
WSACleanup DD RVA _WSACleanup
WSAGetLastError DD RVA _WSAGetLastError
getpeername DD RVA _getpeername
bind DD RVA _bind
listen DD RVA _listen
accept DD RVA _accept
recv DD RVA _recv
send DD RVA _send
setsockopt DD RVA _setsockopt
shutdown DD RVA _shutdown
closesocket DD RVA _closesocket
DD 0
ws2_name: DB 'WS2_32.DLL', 0
_socket: DB 0,0,'socket',0
_htons: DB 0,0,'htons', 0
_WSAStartup: DB 0,0,'WSAStartup',0
_WSACleanup: DB 0,0,'WSACleanup',0
_WSAGetLastError: DB 0,0,'WSAGetLastError',0
_getpeername: DB 0,0,'getpeername',0
_bind: DB 0,0,'bind',0
_listen: DB 0,0,'listen',0
_accept: DB 0,0,'accept',0
_recv: DB 0,0,'recv',0
_send: DB 0,0,'send',0
_setsockopt: DB 0,0,'setsockopt',0
_shutdown: DB 0,0,'shutdown',0
_closesocket: DB 0,0,'closesocket',0
Should i use (add esp, x) for each function ... ?? is this the problem?
or the problem is about function linking ?
i found the problem ... the problem is linking ... my linking is wrong because i used the default macro of fASM (for linking) and it's worked without any problem ... now what is the problem of my linking ?!!!!!
Related
I'm trying to access and print the elements of arr found in the data section below in nasm using printf from c, but every time I get (.text+0x4e): undefined reference to `printf.
can anyone tell me the problem?
xor rcx, rcx
print_array:
cmp rcx, r8
jz exit
mov rax, [arr+rcx]
inc r8
push rax ; caller-save register
push rsi ; caller-save register
mov rdi, char_format ; set 1st parameter (format)
mov rsi, r8 ; set 2nd parameter (current_number)
xor rax, rax ; because printf is varargs
call printf ; printf(format, current_number)
pop rsi ; restore caller-save register
pop rax ; restore caller-save register
jmp print_array
exit:
mov rax, 0
pop rbp
section .data
arr: dd 30, 10, 6, 4, 8, 15, 1, 60, 7, 19, 17, 13, 11
ent: dd "enter the number of items", 10
char_format: db "%c", 10, 0
decimal_format: db "%d", 10, 0
string_format: db "%s", 10, 0
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.
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().
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)
I am trying to teach myself some NASM x86 assembly using some Unix system calls. I am trying to create a simple TCP server and I have the code working up until the send() command. I can connect via telnet but I get a segfault once my code reaches the point where it tries to send a response to the client.
This is the segment of code that is producing a segfault:
; push on to stack for send
push dword 0
push dword [start_len]
push dword [start]
push dword [socket]
; send something back
; THIS IS WHERE THE SEGFAULT OCCURS
mov eax,102
mov ebx,9 ; send is 9
mov ecx,esp
int 80h
Here is the full source code if anyone wants to look at it. Any help would be greatly appreciated!
; constants go here
section .data
start: db 'Starting Socket...',0
start_len: equ $-start
; variables go here
section .bss
socket: resd 1 ; store the fd for the socket
socket_address: resd 2 ; socket address
; starttttt
section .text
global _start
open_socket:
; print we are starting
mov eax,4
mov ebx,1
mov ecx,start
mov edx,start_len
int 80h
; push values to stack to make call
; values go in in opposite order, so when they are popped
; it is the correct order
; below is for a tcp socket
push dword 6
push dword 1
push dword 2
; make call to open socket
mov eax,102 ; 102 is the call to open socket
mov ebx,1 ; sub call, socket()
mov ecx,esp
int 80h
; store the file descriptor for the socket
mov dword[socket],eax
; this is the socket address to bind to
push dword 0x00000000 ; localhost (127.0.0.1)
push dword 0x2823 ; port 9000
push word 2 ; AF_INET (IPv4)
mov [socket_address],esp ; move to our socket address variable
; setup parameters for bind call bind(socket, socket_address, 16)
push dword 16
push dword [socket_address]
push dword [socket]
; call subcall for socket to bind
mov eax,102; sys_socket
mov ebx,2 ; subcall 2 = bind()
mov ecx,esp ; push vars from stack to params
int 80h
; setup parameters for listen()
push byte 20
push dword [socket]
; call listen()
mov eax,102 ; socket call
mov ebx,4 ; subcall listen()
mov ecx,esp ; move stack as variables
int 80h
; now we have to accept incoming connections...
; setup the call
push 0
push 0
push dword [socket]
; call accept()
mov eax,102
mov ebx,5
mov ecx,esp
int 80h
; push on to stack for send
push dword 0
push dword [start_len]
push dword [start]
push dword [socket]
; send something back
; THIS IS WHERE THE SEGFAULT OCCURS
mov eax,102
mov ebx,9 ; send is 9
mov ecx,esp
int 80h
; function to exit the program
exit:
mov eax,1
mov ebx,0
int 80h
; main function to be called
_start:
; open it
call open_socket