Cannot find segmentation fault in insertion sort code for arm-v8 assembly - linux

I have generated an array of random integers between 0 and 256 and I tried to sort them using an insertion sort however at some point along the line I messed up and got an error "segmentation fault (core dumped)
I have no idea exactly why I am getting this but I believe the problem has something to do with the macro j representing a loop counter. I have been working away at this for a few hours and I have been stuck on this for a while.
EDIT: After using the debugger I have isolated the problem to be the line:
bl printf
right at the very end when it tries to print the second sorted value.
I have tried to allocate more ram up to 416 bytes, I have tried moving the top2 loop label around (which is the loop that uses j as it's counter) and the farthest i have gotten is for the program to print the first element of the sorted array and then give the error
ALLOC =-(16+400*1)&-16
.equ DEALLOC, -ALLOC
size = 50
define(arraybase, x19)
i .req x20
define(j, x21)
define(temp, w22)
define(sort1, w23)
print1: .asciz "Array[%d]: %d\n"
print2: .string "\nSorted Array: \n"
print3: .asciz "top value equals %d \n"
...
mov i, 0
top1: add i, i, 1
ldrb temp, [arraybase, i ]
mov j, i
top2: mov x25, j
sub x25, x25, 1
ldrb sort1, [arraybase, x25 ]
cmp temp, sort1
b.ge skip1
strb sort1, [arraybase, j]
skip1: sub j, j, 1
cmp j, 0
b.gt top2
strb temp, [arraybase, j]
cmp i, size-1
b.lt top1
printing:
ldr x0, =print2
bl printf
mov i, 0
ldr x0, =print1
tprint: mov x1, i
ldrb w2, [arraybase, i]
bl printf
add i, i, 1
cmp i, size-1
b.le tprint
mov x0, 0
ldp x29, x30, [sp], DEALLOC
ret
The array should be printed in the random order it was initialized in, then it should print the value at the top of the array, then it is meant to print the sorted array in increasing order.
The exact error message I got was:
Segmentation fault (core dumped)
This appears after it prints the first value of the sorted array

Related

problem with a code "initializer magnitude too large for specified size" on assembler

code works in the following way:it solves the following problem "2d/a-21bd". then it should open a message box, where the result is shown. here is the code:
;include \masm64\include64\masm64rt.inc
.data ;data for the code
a dq 1 ;operand a
b dq 0 ;operand b
c dq 21 ;number 21
d dq 5 ;operand d
q dq 2 ;number 2
res dq 0 ;result
title1 db "Решение уравнения. masm64",0 ;title for the message box
txt1 db "Уравнение 2d/a-21bd",10,
"Результат: %d",10,"Адрес переменной в памяти: %ph",10,10,
"Автор:Тарадай Святослав",0 ;text for the message box
buf1 dq 3 dup(0),0 ;sorry, i don't know what does this code do. if you can say, i'll be happy
.code ;code area. solving a problem 2d/a-21bd
entry_point proc
mov rax ,b ;moving operand b to rax to make a second part of a problem
mul c ;multiplying on 21
mul d ;multiplying on d, or 5
mov rsi, rax ;moving the result to rsi
mov rax,d ;moving operand d, or 5 to rax, making a first part of the problem
mul q ;multiplying on 2
xor rdx,rdx ;preparing rdx to division
div a ;division on a, or 1
sub rax,rsi ;substraction of rsi(21bd) from rax(2d/a)
mov res,rax ;moving result on res
invoke wsprintf,ADDR buf1,ADDR txt1,res, ADDR res1 ;transform function
invoke MessageBox,0,ADDR buf1,ADDR title1,MB_ICONINFORMATION ;output function
invoke ExitProcess,0
entry_point endp
end
but i've got following errors with the invoke thing:
Assembling: C:\masm64\bin64\a1.asm
C:\masm64\bin64\a1.asm(27) : error A2071:initializer magnitude too large for specified size
C:\masm64\bin64\a1.asm(27) : error A2008:syntax error : invokeC:\masm64\bin64\a1.asm(28) : error A2071:initializer magnitude too large for specified size
C:\masm64\bin64\a1.asm(28) : error A2008:syntax error : invokeC:\masm64\bin64\a1.asm(29) : error A2071:initializer magnitude too large for specified size
C:\masm64\bin64\a1.asm(29) : error A2008:syntax error : invokeLINK : fatal error LNK1181: cannot open input file 'C:\masm64\bin64\a1.obj'
if you can help me with this one, because i'm a new one in assembler, i'll be very happy
The problem was with the compilation.i used the makeit.bat wit RC. just changing it to simple makeit.bat solves the problem

Two digit string number Assembly

So I have to strings s1 and s2 and I have two obtain the string d that contains the maximum numbers for each of the positions of s1 and s2.
For example:
S1: 1, 3, 6, 2, 3, 10
S2: 6, 3, 11, 1, 2, 5
D: 6, 3, 11, 2, 3, 10
So this is the code
bits 32
global start
extern exit,printf
import exit msvcrt.dll
import printf msvcrt.dll
segment data use32 class=data
format db "%s",0
s1 db "1","3","6","2","3","10"
l equ $-s1
s2 db "6","3" ,"11","1","2", "5"
d times l db 0
segment code use32 class=code
start:
mov esi,0
mov edi,0
cld
Repeta:
mov al,[s1+esi]
mov bl,[s2+esi]
cmp al,bl
jg et1
mov[d+edi],bl
inc edi
inc esi
jmp et2
et1:
mov[d+edi],al
inc edi
inc esi
et2:
cmp esi,l
jne Repeta
push d
push format
call[printf]
add esp,4*2
push dword 0
call [exit]
The problem is that when it reaches a double digit element(10 or 11) it takes only the first digit(1) and compares it with the number from the other string on the same position and after that it takes the second digit and compares it with the next number from the other string.
How can I solve this?
it says that it should be a string of bytes
The phrase "of bytes" very strongly implies array to me. Ask your instructor for clarification, but I think s1: db 1, 3, 6, 2, 3, 10 is what you're supposed to be working with, so the elements are fixed width single byte integers. (And not ASCII strings at all).
This means you can use a simple pairwise max like SSE2 pmaxub (for unsigned bytes) or SSE4.1 pmaxsb (for signed bytes).
segment data use32 class=data
format db "%s",0
s1 db 1, 3, 6, 2, 3, 10
l equ $-s1
s2 db 6, 3, 11, 1, 2, 5
d times l db 0
start:
movq xmm1, [s1] ; load all 6 elements, plus 2 bytes past the end but that's ok. We ignore those bytes
movq xmm2, [s2]
pmaxub xmm1, xmm2 ; element-wise vertical max
;but avoid storing outside of 6-byte d
movd [d], xmm1 ; store first 4 bytes of the result
pextrw [d+4], xmm1, 2 ; store bytes 4 and 5 (word 2 of xmm1 = 3rd word)
... ; the result isn't a string, you can't print it with printf.
For byte counts that aren't a multiple of 2, e.g. if l was 7, you could use this instead of pextrw:
psrldq xmm1, 3 ; bring the data you want to store down into the low 4 bytes of the register
movd [d+4], xmm1 ; 4-byte store that overlaps by 1
BTW, I realize that you're intended to loop over the elements 1 byte at a time. Maybe use cmp cl, al / cmovg eax, ecx / mov [edi], al to store what was originally in cl if cl > al (signed), otherwise store what was originally in al.
I think your loop structure is a bit broken, because you have one path that doesn't store to d. You always need to store to d, regardless of which source was greater.

MIPS assembly: print the Fibonacci sequence (with syscalls)

Good afternoon! I have a MIPS instruction program that simulates the Fibonacci sequence. My program works by entering in a number (n) that will determine how many iterations the program will work through. The program spits out the correct results but i was wondering how i could output each sequence of number(s) of the Fib method. Heres my code first :
.data
msg1:.asciiz "Give a number: " # message for fib(n)
.text
main:
li $v0,4 #read string
la $a0,msg1 # set iterations to msg1 value
syscall
li $v0,5 # read an int
syscall
add $a0,$v0,$zero #move to $a0
jal fib #call fib
add $a0,$v0,$zero # add result into argument
li $v0,1 # output integer
syscall
li $v0,10
syscall
fib:
#a0=y
#if (y==0) return 0;
#if (y==1) return 1;
#return( fib(y-1)+fib(y-2) );
addi $sp,$sp,-12 #save in stack
sw $ra,0($sp) # save return adress to stack
sw $s0,4($sp)# save msg value
sw $s1,8($sp)# save fib(y-1)
add $s0,$a0,$zero # store msg value into s0
addi $t1,$zero,1 # comparable value
beq $s0,$zero,return0 # if s0 == 0 jump to return 0 block
beq $s0,$t1,return1# if s0 == t1 (1) jump to return 1 block
addi $a0,$s0,-1 # else decrement initial value by 1
jal fib
add $s1,$zero,$v0 #s1=fib(y-1)############################syscall needed#########################################
addi $a0,$s0,-2 # subtract two
jal fib #v0=fib(n-2)
add $v0,$v0,$s1 #v0=fib(n-2)+$s1 ##########################syscall needed#################################
exitfib:
lw $ra,0($sp) #read return adress from stack
lw $s0,4($sp) # read msg value from stack
lw $s1,8($sp) # read fib(y-1) from stack
addi $sp,$sp,12 #bring back stack pointer
jr $ra
return1:
li $v0,10 # return 1 to result
j exitfib
return0 : li $v0,0 # return 0 to result
j exitfib
Hopefully you saw, but i have two lines commented where i believe a syscall would be necessary to output each number per sequence ($s1 = fib(y-1))($v0 = fib(n-2) - $s1). I tried loading the correct code into the result register to output an integer but it still gives me problems (not displaying the process before the final result). How could i correctly implement a syscall so that i can print each number the fib method calculates before reaching a final result?

How to increment a letter in string in lc3?

I am writing an LC3 program that increments each letter of a three-letter word stored in memory following the program. 'a' becomes 'd', 'n' becomes 'q', 'z' becomes 'c', etc.
I am using this as LC3 Assembly a reference
Here is my code so far
.orig x3000
ADD R1, R1, #3
LEA R2, STRING
HALT
STRING .STRINGZ "anz"
.END
I was able to figure out how to declare a string of characters in LC3 from my reference. However does anyone how to do the actual incrementation or have any references that I could use to figure out how to do it?
Using a while loop, I was able to get it to increment each char of the string until a null value is found. I didn't code it to loop back around (z becoming c) but this should get you started.
;tells simulator where to put my code in memory(starting location). PC is set to thsi address at start up
.orig x3000
MAIN
AND R1, R1, #0 ; clear our loop counter
WHILE_LOOP
LEA R2, STRING ; load the memory location of the first char into R1
ADD R2, R2, R1 ; Add our counter to str memory location. R2 = mem[R1 + R2]
LDR R3, R2, #0 ; Loads the value stored in the memory location of R2
BRz END_WHILE ; If there is no char then exit loop
ADD R3, R3, #3 ; change the char
STR R3, R2, #0 ; store the value in R3 back to the location in R2
ADD R1, R1, #1 ; add one to our loop counter
BR WHILE_LOOP ; jump to the top of our loop
END_WHILE
HALT
; Stored Data
STRING .STRINGZ "anz"
.END

Using relative values in array sorting ( asm )

I need to sort through an array and sort each individual row in array to be in ascending order. I doesn't seem to going so well (surprise!) as I keep getting hit with two errors:
a2101: cannot add two relocatable labels
and
a2026: constant expected
Here's my sort, it makes sense to me, but I think I'm still trying to implement high-lvl language techniqes into assembly. Is there a way to get around not being able to use relative values? (the array is 7 rows by 9 columns, btw).
mov cx, 7; cx = number of rows
outer: ; outer loop walk through the rows
push cx
mov cx, 9
mov row, cx ;rows
middle: ; middle-loop walk through the columns
push cx
sub cx, 1 ;cx = cx-1
mov column, cx ;columns
inner: ;inner loop - compare and exchange column values
cmp mArray[row*9 + column], mArray[row*9 + column+1]
xchg mArray[row*9 + column+1], mArray[row*9 + column]
; compare and exchange values from mArray table
inc column
loop inner
pop cx
loop middle ;end middle loop
pop cx
loop outer ; end outer loop
ret
Thanks for any help.
The following lines are problematic:
cmp mArray[row*9 + column], mArray[row*9 + column+1]
xchg mArray[row*9 + column+1], mArray[row*9 + column]
Unlike HLL, assembly does NOT allow for arbitrary expressions in place of constants or variables. That's why HLL's were invented in the first place. Calculate the offset in the registers before using:
mov ax, row
mov bx, ax
shr bx, 3 ; bx = row*8 now
add bx, ax ; bx = row*9 now
add bx, column ; bx = row*8+column now
mov dx, [bx] ;first comparand
inc bx
cmd dx, [bx] ; that's your compare!
Also, you don't use any branching; the cmp instruction is utterly pointless; you waste its result, and xcng is not executed conditionally. Read up on conditional jump commands (jz/jnz etc.).
Also, I seriously hope this is an exercise, not a real project. If it's for real, please reconsider using assembly. For something as trivial as this, assembly is a wrong, wrong choice. Espec. considering how bad you are at it.

Resources