mov edx overwrites cx register - linux

I'm trying to print Hi 10 times. This is my code.
section .data
msg db "Hi"
section .text
global _start
_start:
mov cx, 10
L1:
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, 3
int 0x80
dec cx
jnz L1
mov eax, 1
mov ebx, 0
int 0x80
gdb reports that mov edx, 3 overwrites the cx register to some crazy value and so the loop keeps going forever.
What am i doing wrong? Is it because they are the same register?
How does one program in assembly with so few registers?
Compiling on centos with nasm and ld
Thanks

You're looking at the wrong line. The problem is "mov ecx, msg". ECX is the extended register of which CX is the lower part, so you're writing over it.
It's best to save your loop counter on the stack, because who knows that the 'int' call might change. Add 'push cx' (or ecx) after 'L1:'. and 'pop cx' after the 'int' call to preserve the contents of the register.

This code fixes it:
section .data
msg db "Hi"
counter dw 10
section .text
global _start
_start:
L1:
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, 2
int 0x80
mov cx, [counter]
dec cx
mov [counter], cx
jnz L1
mov eax, 1
mov ebx, 0
int 0x80
move the value into a variable and then dec it then put it back

Related

Assembly NASM - AND Mask

When I run this program it says:
jdoodle.asm:9: error: invalid combination of opcode and operands
The problem is the AND al, ah. The rest of the code should be correct, I just need to know how to solve this problem because as it seems I can't do an AND between 2 registers.
section .text
global _start
_start:
call _input
mov al, input
mov ah, maschera
and al, ah
mov input, al
call _output
jmp _exit
_input:
mov eax, 3
mov ebx, 0
mov ecx, input
mov edx, 1
int 80h
ret
_output:
mov eax, 4
mov ebx, 1
mov ecx, input
mov edx, 1
int 80h
ret
_exit:
mov eax, 1
int 80h
section .data
maschera: db 11111111b
segment .bss
input resb 1
MASM/TASM/JWASM syntax is different from NASM. If you want to load/store data at an address you need to explicitly use square brackets. If you want to use the MOV instruction to place the address of a label in a variable you do not use square brackets. Square brackets are like a de-reference operator.
In 32-bit code you will want to ensure addresses are loaded into 32-bit registers. Any address above 255 won't fit in an 8 byte register, any address above 65535 won't fit in a 16-bit register.
The code you were probably looking for is:
section .text
global _start
_start:
call _input
mov al, [input]
mov ah, [maschera]
and al, ah
mov [input], al
call _output
jmp _exit
_input:
mov eax, 3
mov ebx, 0
mov ecx, input
mov edx, 1
int 80h
ret
_output:
mov eax, 4
mov ebx, 1
mov ecx, input
mov edx, 1
int 80h
ret
_exit:
mov eax, 1
int 80h
section .data
maschera: db 11111111b
segment .bss
input resb 1

Linux Assembly segmentation fault print using loop

I'm writing an assembly program that would print even numbers between 0-9 using a loop. I encountered this problem, segmentation fault while running the code. I check other answers on the site but couldn't find an answer that satisfies my issue.
I suspect that the function nwLine might be the source of the problem.
;;this program prints even numbers from 0-8 using loop function
section .text
global _start
cr db 10
_start: ;tell linker entry point
mov ecx, 5
mov eax, '0'
evenLoop:
mov [evnum], eax ;add eax to evnum
mov eax, 4
mov ebx, 1
push ecx
mov ecx, evnum
mov edx, 1
int 80h
call nwLine
mov eax, [evnum]
sub eax, '1'
inc eax
add eax, '2'
pop ecx
loop evenLoop
nwLine: ;function to move pointer to next line
mov eax,4 ; System call number(sys_write)
mov ebx,1 ; File descriptor 1 - standard output
mov ecx, cr
mov edx, 1
int 80h ; Call the kernel
ret
mov eax,1 ;system call number (sys_exit)
int 80h ;call kernel
section .bss
evnum resb 1
if anyone knows how to solve the problem with the nwLine function, please tell me.

NASM: SegFault on MOV ECX

I'm trying to make a very simple assembly program run, however I seem to get segfaults whatever I do.
Here is my code (should print 'a' on a linux machine)
section .data
buffer times 50 db 97
pointer db 0
section .text
global _start
_start:
mov ECX , pointer
mov EDX , [buffer + ECX]
mov EAX , 4
mov EBX , 1
mov ECX , EDX
mov EDX , 1
int 0x80
It causes a segfault on the first MOV but it seems obvious to me that it should work.
I reduced it to almost nothing and it still segfault.
section .data
msg db "hello"
section .text
global _start
_start:
mov EAX,1
I've run this succesfully:
section .text
global _start
_start:
mov ax, 0b
dec ax
sub ax, 11111111b
mov bx, 97
add ax, bx
mov [INVENTORY], ax ; put a in first inventory pos
mov eax, 4
mov ebx, 1
mov ecx, INVENTORY
mov edx, 1
int 0x80
mov ax, [INVENTORY]
add ax, 1
mov [INVENTORY + 1], ax ; put b in second inventory pos
mov [VAR], ax
mov eax, 4
mov ebx, 1
mov ecx, VAR
mov edx, 1
int 0x80
mov eax, 4
mov ebx, 1
mov ecx, '\n'
mov edx, 1
int 0x80
mov eax,1
int 0x80
_newline:
section .data
VAR DW 0
INVENTORY TIMES 8 DW 0
Is it possible that it has to do with the symbols I use for newlines or tabs? I generate the assembly from java and I use \t for tabs and \n for new lines (and spaces so it doesn't look too bad.
I'm using NASM and I'm running it here:
https://www.tutorialspoint.com/compile_assembly_online.php
Thank you!
If you are just trying to print out a set of 'a's.
section .data
buffer times 50 db 97
len.buffer equ $-buffer
pointer db 0
section .text
global _start
_start:
; ssize_t write(int fd, const void *buf, size_t count);
; i386 ebx ecx edx esi edi ebp
mov EAX , 4 ; write syscall
mov EBX , 1 ; std out
lea ecx, [buffer] ; buffer
mov edx, len.buffer ; size
int 0x80
_exit:
mov eax, 1 ; exit syscall
int 0x80
output:
./yvon_001
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\ndavid#ubuntuserver00A:~/asm$ ./yvon_001
In the end #MichealPetch was right, I needed to add an EXIT syscall at the end of the code. The sample I tried still did a SEGFAULT because I was moving pointer instead of [pointer] in a registry.
Thanks for comments and answers!

System call in nasm not working

I try write follow code, use int 0x80 to print number 5 but it don't print anything.
segment .bss
num1 resb 1
section .text
global _start
_start:
mov dword [num1],5
add [num1],byte '0'
mov ecx, dword [num1]
mov eax, 4
mov ebx, 1
mov edx, 1
int 0x80
mov eax, 1
int 0x80
System call 4 expects ecx to contain a pointer to a NUL-terminated string.
So instead of mov ecx, dword [num1] you should use mov ecx, num1.

How to return of Called Functions multiple times/NASM

I am trying to return of a function, which also calls a function, but I get a SIGSEV.
MY ASSEMBLER: Linux, Unix nasm 64 +ld
sample:
;something
call myFunction
myFunction:
call func2
ret
func2:
;do sth
ret
One possibility is to push the Pointer to the stack, but the Stack is Limited, isn't it? And I need a solution for doing this n-times, whereas n should be able to stand for infinity.
Another possibility is to call the function and do a label for returning, but when I hava a function, which is called by different routines, I'm runnning into another problem :(
Is there an elegant way of doin' this?
Ox, so I got the suggestion, I should try it as it could work.
I tried it with this code:
section .data
test : db "test", 0x0a; length: 5
input: db "433", 0x00
blank: db 0xa
section .bss
section .text
global _start:
_start:
mov eax, 4
mov ebx, 1
mov ecx, input
mov edx, 4
int 80h
mov eax, input
call strToInt
mov [input], eax
mov eax, 4
mov ebx, 0
mov ecx, test
mov edx, 5
int 80h
ret
mov eax, 1
mov ebx, 0
int 80h;
strToInt:
mov ebx, 0; counter
mov ecx, 0; ecx is 4 the result
conversion_loop:
cmp BYTE[eax], 0
jz leave_loop
sub byte[eax], 48
inc eax
inc ebx
cmp ebx ,9
jge leave_loop
jmp conversion_loop
leave_loop:
call hey
ret
hey:
mov eax, 4
mov ebx, 0
mov ecx, test
mov edx, 5
int 80h
ret
(sorry for the code, this long -.-)
the result is:
433test
test
SEGFAULT
Does this clarify this a bit?
Look at this lines:
int 80h
ret
I think this ret is jump out of your code segment. You must remove ret to call sys_exit function to complete programm gracefully.

Resources