assembly read from file segmentation - file

.data
pn: .string "input.bin"
string: .string "%f\n"
buf_size = 8
alloc = -(16+buf_size)&-16
dealloc = -alloc
buf_s = 16
.text
.balign 4
.global main
main: stp x29,x30,[sp,alloc]!
mov x29,sp
mov w0,-100
ldr x1,=pn
mov w2,0
mov w3,0
mov x8,56
svc 0
cmp w0,0
mov w19,w0
mov w24,w19
mov x20,0
mov x21,8
b.ge open_ok
open_ok:
mov w0,w19
add x1,x29,x21
mov x2,8
mov x8,63
svc 0
ldr d0,[x1]
ldr x0,=string
bl printf
add x21,x21,8
cmp x20,199
add x20,x20,1
b.lt open_ok
mov w0,w24
mov x8,57
svc 0
mov x1,x0
ldr x0,=string
bl printf
ldp x29,x30,[sp],dealloc
ret
I'm trying to write a program that reads from a file "input.bin" and displays the contents of the file. It reads the file correctly and displays the data the way it is supposed to but right at the end it crashes due to a segmentation fault. Not sure whats going on. Input contains the numbers 0.5 to 100, incremented by 0.5 each time. It prints it out correctly and then crashes right at the end.

Related

Accessing an array from the stack

I am new to assembly programming and trying to write a simple assembly level code where the intention is to send the base address of an array to a stack and access it within a procedure to find out the smallest element The array is a 5 byte array with 5 elements. Unfortunately after passing the address of the array within stack I am unable to access it back through BP register. The code snippet is as below and I am using the 8086 emulator. Any help will be very much appreciated.
START:
MOV AX,#data
MOV DS,AX
;MOV AX,STACK
;MOV SS,AX
;MOV SP,OFFSET TOP_STACK
MOV BX,OFFSET ARRAY
PUSH BX
PUSH BP
MOV BP,SP
MOV BX,[BP+4]
MOV AX,[BX]
Till the second last line I am able to follow the execution and the system is showing the correct address of the data segment. However when the last line executes I expect that the first element of the array will be available ( i.e 10 ) but in reality I get only 0.
Here is the full code:
.stack 100h
.data
ARRAY DB 10H,05H,20H,60H,35H
.code
START:
MOV AX,#data
MOV DS,AX
MOV BX,OFFSET ARRAY
PUSH BX
PUSH BP
CALL PROC1 ; CALLING POINT OF THE PROCEDURE
MOV AH,4CH
INT 21H
PROC1 PROC NEAR
MOV BP,SP
MOV BX,word[BP+3] ; ARRAY BASE ADDRESS
XOR AX,AX
MOV AL,BYTE[BX]
MOV CX,04
SMALL:
MOV AH,BYTE[BX]
CMP AL,AH
JG SWAP
INC BX
LOOP SMALL
JMP EXITPROC
SWAP:
MOV AL,AH
INC BX
JMP SMALL
EXITPROC:
POP BP
POP BX
RET
PROC1 ENDP
CODE ENDS
END START

Assembly - Correcting a Source Program

I am given this assignment:
This program is supposed to search an array of bytes for the value 0xf2. When it finds 0xf2, it should save its location (i.e. address) into the integer variable “f2Address”. If it does not find the value of 0xf2 in the array, it should put a value of 0x00 into the variable “f2Address”. This program has some bugs in it. Fix this program so that it does work properly.
The original code given was:
.label TABLE1_LOC_START
TABLE1 .byte 0xff,0xfe,0xfd,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,0xf6,0xf5,0xf4,0xf3,0xf2,0xf1,0xf0
TABLE1_ST .word TABLE1_LOC_START
RESET mov.w #0280h,SP
mov.w #WDTPW+WDTHOLD,&WDTCTL ; stop watchdog timer
mov &TABLE1_ST, R10
mov #0xf2, R11
mov #0x08, R12
again inc R10
cmp 0(R10), R11
je found
dec R12
jz again
found mov R10, &f2Address
endProgram jmp endProgram
In an attempt to solve this problem I changed the 'je' to 'jeq' and I added 'mainLoop' to the code. After my modifications I had this code:
.label TABLE1_LOC_START
TABLE1 .byte 0xff,0xfe,0xfd,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,0xf6,0xf5,0xf4,0xf3,0xf2,0xf1,0xf0
TABLE1_ST .word TABLE1_LOC_START
mainLoop mov &TABLE1_ST, R10
mov #0xf2, R11
mov #0x08, R12
again inc R10
cmp R10, R11
jeq found
dec R12
jnz again
found mov R10, &f2Address
endProgram jmp endProgram
When I step through it, R12 eventually decrements to zero. Once it does, it means that the value of 0xf2 wasn't found, so it should place a 0x00 in the 'f2Address'. But instead of placing a zero there, it simply keeps on moving through the instructions without changing/adding any values anywhere.
I am not quite sure what to do or where to go next from here. It is using CodeComposer on the MSP430.
Figured it out:
mainLoop mov &TABLE1_ST, R10
mov #0xf2, R11
mov #0x10, R12
again inc R10
cmp 0(R10), R11
jeq found
dec R12
jnz again
mov #0x00, &f2Address
found mov R10, &f2Address
endProgram jmp endProgram

ARM Assembly: Array Address not loading properly?

I am working on this homework assignment, and my professor is stumped (as I am too) on why my array address refuses to load into R2. He said my code is correct, but he is absolutely uncertain about why it is behaving this way. It used to load 10 constantly, however, under the branch manageArray:, it should be overwriting the old use of R2. I even tried re-initializing the value, and it never worked.
We have run it under ARMSim, and code it as a .s file under notepad++. If anyone can point out what the potential cause is and why (because I absolutely want to learn from this, not look for answers!), I would be forever in your debt!! We have sat for nearly an hour and sent back so many emails over this, we're just frustrated and lost on where to go. It always loads a value of 10 or 11, but never the array address.
This is my code as of now. Basically, it's meant to check for odds (and terminate if true), else, add all of the array and print that total out. Semi-colons (;) are comments in assembly, which is weird.
.data
oddPrompt: .asciz "This Array is odd: we are terminating this thing.\n\r"
.align
terminatedPrompt: .asciz "YOU HAVE BEEN TERMINATED!\n\r"
.align
totalPrompt: .asciz "Total : \n\r"
.align
arrayLength = 10
;determineArrayOdds = arrayLength
array: .skip arrayLength*4
.text
mov r0,#0
mov r1,#0
mov r2,#arrayLength
mov r3,#0
mov r6,#0 ;element 1
mov r7,#0 ;element 2
mov r8,#0 ;added values from R7 and R6
mov r9,#0 ;grandTotal
verifyOdds:
sub r2, r2, #2
cmp r2,#2
blt errorForOdd
bgt verifyOdds
beq manageArray
errorForOdd:
;It's odd, so we skip calculating, because we never hit 2
mov r0,#1
ldr r1,=oddPrompt
swi 0x69
swi 0x11
manageArray:
ldr r2,=array
loop:
;Here, populate and add array elements.
;R2 will be re-purposed for this to conserve space.
swi 0x6d
and r0,r0,#0xff ;get time tick UP to 255 (via hex)
mov r1,r0
mov r0,#1
;swi 0x6b
str r1,[r2],#4
cmp r4,#arrayLength
addlt r4,r4,#1
blt loop ;ORIGINAL 49, now 50
moveq r4,#0
ldreq r2,=array
beq addArray
addArray:
ldrlt r6,[r2,#0] ;element 1
ldrlt r7,[r2,#4] ;element 2
add r2,r2,#8
add r8,r7,r6 ;store to r8
;mov r9,r8
add r9,r8,r9
cmp r4,#arrayLength
addlt r4,r4,#1
blt addArray
beq total
total:
mov r0,#1
ldr r1,=totalPrompt
swi 0x69
mov r1,r9 ;r1 has to hold the total to print
mov r0,#1 ;r0 must be set to true
swi 0x6b ;PRINT TOTAL NOW
swi 0x11
terminate:
mov r0,#1
ldr r1,=terminatedPrompt
swi 0x69
swi 0x11

Print an integer NASM

I'm trying to read 3 numbers from the user and I store them in an array then I show them using paul carter functions in NASM here is my code :
%include "asm_io.inc"
SECTION .bss
tab resb 3
SECTION .data
msg db "Un nombre :",10
SECTION .text
global main
main:
mov esi,tab
xor ecx,ecx
get_data:
mov eax,msg
call print_string
call read_int
mov [esi+ecx],eax
inc ecx
cmp ecx,3
jne get_data
call print_nl
xor ecx,ecx
mov edi,tab
print_data:
mov eax,[edi+ecx]
call print_int
inc ecx
cmp ecx,3
jne print_data
mov eax,1 ; exit code
int 0x80 ; call exit
The problem is that is doesn't execute print_data loop and it exit .
Just to be clear. Are these the changes you did? Even if the ECX register is not used as an input or output for a particular function it might still be used internally. That's what Michael meant with 'ecx is typically caller-saved'
%include "asm_io.inc"
SECTION .bss
tab resd 3
SECTION .data
msg db "Un nombre :",10,0
SECTION .text
global main
main:
mov edi,tab
xor ecx,ecx
get_data:
push ecx
mov eax,msg
call print_string
call read_int
mov [edi+ecx*4],eax
pop ecx
inc ecx
cmp ecx,3
jne get_data
call print_nl
mov esi,tab
xor ecx,ecx
print_data:
push ecx
mov eax,[esi+ecx*4]
call print_int
pop ecx
inc ecx
cmp ecx,3
jne print_data
mov eax,1 ; exit code
int 0x80 ; call exit

NASM programming - Segmentation fault(core dumped)

My program is very simple. It takes two input from the user:
an array (only 2 digit positive numbers)
a 2-digit number to be searched
output: number of times the given no. occurs in array
sample
Enter the size of array
03
Enter a number
10
Enter a number
12
Enter a number
10
Enter the number to be searched
10
Your number appeared 02 times
everything seems to be working fine but this programs gives the error
0segmentation fault(core dumped)
Please help me solve this problem
enter code here
section .data
msg1: db "Enter the size of array",10
len1: equ $-msg1
msg2: db "Enter a number",10
len2: equ $-msg2
msg3: db "Enter the number to be searched :"
len3: equ $-msg3
msg4: db "Your entered number appeared "
len4: equ $-msg4
section .bss
temp: resb 1
array: resb 100
d1: resb 1
d0: resb 1
size: resb 1
num: resb 1
ele: resb 1
time: resb 1
section .text
global _start:
_start:
;Getting the size of array
mov eax,4
mov ebx,1
mov ecx,msg1
mov edx,len1
int 80h
mov eax,3
mov ebx,0
mov ecx,d1
mov edx,1
int 80h
mov eax,3
mov ebx,0
mov ecx,d0
mov edx,2
int 80h
sub byte[d1],48
sub byte[d0],48
mov al,byte[d1]
mov bl,10
mul bl
add byte[d0],al
mov al,byte[d0]
mov byte[size],al
mov byte[temp],al
mov ebx,array
reading:
push rbx ;preserves the value of ebx
mov eax,4
mov ebx,1
mov ecx,msg2
mov edx,len2
int 80h
mov eax,3
mov ebx,0
mov ecx,d1
mov edx,1
int 80h
mov eax,3
mov ebx,0
mov ecx,d0
mov edx,2
int 80h
sub byte[d1],48
sub byte[d0],48
mov al,byte[d1]
mov bl,10
mul bl
add byte[d0],al
mov al,byte[d0]
pop rbx
mov byte[ebx],al
add ebx,1
dec byte[temp]
cmp byte[temp],0
jg reading
mov eax,4
mov ebx,1
mov ecx,msg3
mov edx,len3
int 80h
mov eax,3
mov ebx,0
mov ecx,d1
mov edx,1
int 80h
mov eax,3
mov ebx,0
mov ecx,d0
mov edx,2
int 80h
sub byte[d1],48
sub byte[d0],48
mov al,byte[d1]
mov bl,10
mul bl
add byte[d0],al
mov al,byte[d0]
mov byte[ele],al
mov cl,byte[size]
mov byte[temp],cl
mov byte[time],0
search:
mov al,byte[ebx]
cmp byte[ele],al
jne nf
jmp f
mov cl,0
f: add ebx,1
add byte[time],1
dec byte[temp]
cmp byte[temp],cl
jg search
jmp next
nf:
add ebx,1
add byte[time],0
dec byte[temp]
cmp byte[temp],cl
jg sea rch
jmp next
next:
mov eax,4
mov ebx,1
mov ecx,msg4
mov edx,len4
int 80h
movzx ax,byte[time]
mov bl,10
div bl
mov byte[d1],al
mov byte[d0],ah
add byte[d0],48
add byte[d1],48
mov eax,4
mov ebx,1
mov ecx,d1
mov edx,1
int 80h
mov eax,4
mov ebx,1
mov ecx,d0
mov edx,1``
int 80h
mov eax,1
mov ebx,0
int 80h
global _start
section .text
_start:
mov rax,1
mov rdi,1
mov rsi,msg
mov rdx,msglen
syscall
mov rax,0
mov rdi,0
mov rsi,no1
mov rdx,5
syscall
mov rax,1
mov rdi,1
mov rsi,msg1
mov rdx,msg1len
syscall
mov rax,0
mov rdi,0
mov rsi,no2
mov rdx,5
syscall
mov rax,[no1]
sub rax,30h
mov rbx,[no2]
sub rbx,30h
add rax,rbx
add rax,30h
mov [res],rax
mov rdx,2
syscall
mov rax,1
mov rdi,1
mov rsi,res
mov rdx,2
syscall
mov rax,60h
mov rdx,0
syscall
section .data
msg: db "Enter 1st Number",0x0a
msglen equ $ -msg
msg1: db "Enter 2st Number",0x0a
msg1len equ $ -msg1
msg2:db "Addition is==",0x0a
msg2len equ $ -msg2
section .bss
no1: resb 5
no2: resb 5
res: resb 15
Looking at the source, I noticed one thing. I think your 'search' loop goes out of bounds because you failed to initialize register cl; the instruction mov cl,0 is currently dead code; it is beneath an unconditional jump so it never ever gets executed.
But as I said earlier, when in doubt, use a debugger.

Resources