Intel 8086 Insertion Sort: Skipping numbers within the Array - arrays

So we're currently studying Intel 8086 Insertion Sort Code that our professor showed us. He wanted us to figure out why the code skips the 0th element within the array and the 3rd element in the array from a code that he had taken from the web.
; void isort(int *a, int n)
; sorts the first n elements of a
;
; Parameters
; a - pointer to the array
; n - number of elements to sorts
%define a [ebp + 8]
%define n [ebp + 12]
isort:
enter 0, 0
pusha
mov ecx, 1
for:
mov ebx, ecx
imul ebx, 4
add ebx, a
mov ebx, [ebx]
mov edx, ecx
dec edx
while:
cmp edx, 0
jl while_quit
mov eax, edx
imul eax, 4
add eax, a
cmp ebx, [eax]
jge while_quit
mov esi, [eax]
mov dword [eax + 4], esi
dec edx
jmp while
while_quit:
mov [eax], ebx
inc ecx
cmp ecx, n
jl for
popa
leave
ret
And the sample array was {5, 8, 12, 2, 1, 7}. This is more for understanding the 8086 language since we just started a couple days ago, and I was wondering if anyone could explain how and what might be going wrong.

Consider what the code will do when ECX is 1:
The while loop will be entered with EBX=8 and EDX=0.
The jl while_quit will not be taken, since EDX is 0.
EBX is compared to [EAX]. That is; 8 is compared to a[0], which is 5, so the jge while_quit is taken.
mov [eax], ebx stores 8 at a[0], so your array now contains {8,8,12,2,1,7}. Clearly not what you want, since you've lost one of the original elements.
Apart from the code's logical flaw, those imul instructions are completely unnecessary since you can use scaled indices on the x86. So the sorting code can be simplified to this (I've verified that it sorts the array correctly):
mov ecx, 1
for_:
; esi = &a[holePos]
lea esi,[a + ecx*4]
; valueToInsert = a[holePos]
mov ebx, [esi]
while_:
; Did we reach the beginning of the array?
cmp esi, OFFSET a
jle while_quit
; valueToInsert < a[holePos-1] ?
cmp ebx, [esi-4]
jge while_quit
; a[holePos] = a[holePos-1]
mov eax, [esi-4]
mov [esi], eax
; esi = &a[holePos-1]
sub esi,4
jmp while_
while_quit:
; a[holePos] = valueToInsert
mov [esi], ebx
inc ecx
cmp ecx, 6
jl for_
I'm using MASM syntax, but you get the idea.

Related

Allocate a vector of bytes on stack in assembly x86

I try to put on stack the values contained by array_output but every time I print the values from the stack it prints just 0s.
If I try to print the array simple not using the stack it works.
What I do wrong?
stack_alloc:
sub esp, 1
mov al, byte [array_output + ecx]
mov byte [esp], al
add ecx, 1
cmp ebx, ARRAY_OUTPUT_LEN
cmp ebx, ecx
jg stack_alloc
cmp ebx, ARRAY_OUTPUT_LEN
cmp ebx, ecx
jg stack_alloc
You have a typo in your code. The cmp ebx, ARRAY_OUTPUT_LEN instruction should not be comparing but rather loading the EBX register.
You could correct the problem replacing the cmp with a mov but I would propose to simplify your code and just compare the index in ECX to ARRAY_OUTPUT_LEN. This will require choosing the opposite conditional branch and saves from using the additional register EBX:
xor ecx, ecx
stack_alloc:
sub esp, 1
mov al, [array_output + ecx]
mov [esp], al
inc ecx
cmp ecx, ARRAY_OUTPUT_LEN
jb stack_alloc

View array while debugging assembly code in visual studio

I defined a DWORD variable: arr DWORD 6 Dup(?) and tried to view this array with visual studio debugger.
I did try arr, 6 but it doesn't seem to work while debugging assembly code.
I also tried arr, arr + 1, arr + 2 separately but the result is terribly wrong.
How to view the entire array?
ReadDec_inline proc Uses eax ebx ecx esi edi, n:DWORD, pArr:DWORD
Local OldEsp:DWORD
; Type in n numbers
mov eax,10
mov ebx,n
mul ebx
add eax,n
sub eax,1; add (n-1) space characters
mov OldEsp,esp
sub esp,eax
mov esi,esp
invoke ReadString,esi,45,offset _dword
;Now we extract
mov esi,pArr
mov ecx,n
L1:
mov eax,0
W1:
.If(BYTE PTR[esp + eax] == " " || BYTE PTR[esp + eax] == 0)
mov BYTE PTR[esp+eax],0
jmp quitW1
.Else
inc eax
.EndIf
jmp W1
quitW1:
mov edi,esp
mov ebx,eax
invoke ReadDec, edi, 1
mov [esi],eax ; Fetch the number
add esi,4
add esp,ebx
inc esp ; Next number
loop L1
mov esp,OldEsp
ret
ReadDec_inline endp
proc Uses eax ebx ecx esi edi
Because of the mul ebx instruction, you should add EDX to the list. The result of this multiplication will be in EDX:EAX.
Or better, change
mov eax,10
mov ebx,n
mul ebx
add eax,n
sub eax,1; add (n-1) space characters
into
imul eax, n, 10 + 1
dec eax
invoke ReadString,esi,45,offset _dword
Shouldn't this hardcoded 45 be actually 65? Same as the calculated value?
Your L1 loop depends on the ECX register. I don't think that ECX is a call-preserved register, so probably invoke ReadDec, edi, 1 messes up the count.
mov esi, pArr
mov ecx, n
L1:
mov edi, esp
push ecx ; (1)
xor ebx, ebx
W1:
.If(BYTE PTR[edi + ebx] == " " || BYTE PTR[edi + ebx] == 0)
mov BYTE PTR[edi + ebx], 0
jmp quitW1
.Else
inc ebx
.EndIf
jmp W1
quitW1:
invoke ReadDec, edi, 1
mov [esi], eax ; Fetch the number
add esi, 4
pop ecx ; (1)
add esp, ebx
inc esp ; Next number
dec ecx
jnz L1
I defined a DWORD variable: arr DWORD 6 Dup(?) and tried to view this array with visual studio debugger.
Your invoke ReadDec_inline, 3, offset arr asks for just 3 array elements and you can see their values in the Memory 1 pane. I sincerely thought the issue here was not having specified the whole 6.
However, it fails in the Watch 1 pane because Visual Studio allows you to write expressions in the Watch windows. This means that arr, arr+1, arr+2, and arr+4 are expressions that first retrieve the value at arr (the first array element which has the value 13), and then have some constant added.
I don't use Visual Studio myself, but for MASM you could try:
arr[0] or just arr
arr[4]
arr[8]
or
[arr+0] or just arr
[arr+4]
[arr+8]

Shellsort: Write NASM code from C

I am trying to convert a C function to NASM in 32-bit.
Here is the C function
void shellsort (int *A, int n)
{
int gap, i, j, temp;
for (gap = n/2; gap > 0; gap /=2)
{
for (i= gap; i<n; i++)
{
for (j=i-gap; j>=0 && A[j] > A[j+gap]; j-=gap)
{
temp = A[j];
A[j] = A[j+gap]
A[j+gap] = temp;
}
}
}
}
Here is what I have done so far
%include "asm_io.inc"
SECTION .data
SECTION .bss
SECTION .text
extern printf
global sort
sort:
push ebp ;set up stack frame
mov ebp, esp
sub esp, 0x10 ;assign 16byte space for local variables
firstloop:
mov eax, [ebp+8] ;moves value of n into eax
mov edx, 0 ;prepares for division, remainder init
mov ecx, 2 ;divisor
div ecx ;division, store gap in eax
cmp eax, 0 ;compare if gap with zero
jle firstloopDone ;firstloopDone
firstloopDone:
div ecx
jmp done
secondloop:
mov ecx, eax ;copy value of gap into ecx, i=gap
cmp ecx, [ebp+8] ;compare i with 1st parameter n
jge secondloopDone ;jump to secondloop if greater-equal
inc ecx ;increment i
jmp thirdloop ;jump to thirdloop
secondloopDone:
inc ecx ;increment i
jmp firstloop ;jump to firstloop
thirdloop:
mov edx, ecx ;save i value
sub ecx, eax ;subtract gap from i and store in ecx
cmp ecx, 0 ;compare j with zero
jl thirdloopDone ;if not j>=0, then skip to end of loop
cmp [ebp+12+ecx], [ebp+12+ecx+eax] ;compare A[j] and A[j+gap]
jle thirdloopDone
sub ecx, eax ;subtract gap from j and store in ecx
jmp swap
thirdloopDone:
sub ecx, eax ;j-gap
jmp secondloop ;jump to second loop
swap:
mov edx, [ebp+12+ecx] ;copy A[j] to temp
mov [ebp+12+ecx], [ebp+12+ecx+eax] ;A[j]=A[j+gap]
mov [edp+12+ecx+eax], edx ;A[j+gap]= temp
jmp thirdloop
done:
leave ;destroy stack frame
ret
Needless to say, it doesn't work.
The error message says:
"error: beroset-p-603-invalid effective address"
at line 58 (thirdloop)
cmp [ebp+12+ecx], [ebp+12+ecx+eax]
and at line 71 (swap)
mov edx, [ebp+12+ecx]
I understand that this may be the wrong method. But from the nested loop in C code, I have too many variables to keep so I cant spare any of the registers.
Also, I suspect I may not have a proper understanding of the stack frame, which may show in my code.
Any help, or bug-discovery, would be greatly appreciated. Thanks.
You say you don't have enough registers to play with but you don't even use EBX, ESI, nor EDI. You made provisions for local variables yet you don't use any.
firstloop:
mov eax, [ebp+8] ;moves value of n into eax
mov edx, 0 ;prepares for division, remainder init
mov ecx, 2 ;divisor
div ecx ;division, store gap in eax
The previous code does not need the division. Simplify it with a shift operation. shr eax,1
The next code will always terminate. Use jle done
cmp eax, 0 ;compare if gap with zero
jle firstloopDone ;firstloopDone
firstloopDone:
div ecx
jmp done
Aren't parameters pushed from right to left?
mov ebx, [ebp+8] ;pointer to array A
firstloop:
mov eax, [ebp+12] ;number of elemnets n
You would typically have to code
swap:
mov edx, [ebx+ecx*4] ;TEMP=A[j]
add ecx,eax
xchg edx, [ebx+ecx*4] ;Swap A[j+gap] with TEMP
sub ecx, eax
mov [ebx+ecx*4], edx ;A[j]=TEMP
jmp thirdloop

declaring a string in assembly

I have this assembly code that computes some prime numbers:
#include <stdio.h>
int main() {
char format[] = "%d\t";
_asm{
mov ebx, 1000
mov ecx, 1
jmp start_while1
incrementare1:
add ecx, 1
start_while1:
cmp ecx, ebx
jge end_while1
mov edi, 2
mov esi, 0
jmp start_while2
incrementare2:
add edi, 1
start_while2:
cmp edi, ecx
jge end_while2
mov eax, ecx
xor edx, edx
div edi
test edx, edx
jnz incrementare2
mov esi, 1
end_while2:
test esi, esi
jnz incrementare1
push ecx
lea ecx, format
push ecx
call printf
pop ecx
pop ecx
jmp incrementare1
end_while1:
nop
}
return 0;
}
It works fine but I would like to also declare the 'format' string in asm, not in C code. I have tried adding something like format db "%d\t", 0 but it didn't work.
If all else fails there's always the ugly way:
format_minus_1:
mov ecx,0x00096425 ; '%', 'd', '\t', '\0' in little-endian format
lea ecx,format_minus_1 + 1 ; skip past the "mov ecx" opcode
push ecx
call printf
You cannot define objects inside the _asm block with those directives. The C declaration is allocating space on the stack for you so if you want to do something like that inside the _asm block you need to manipulate the stack pointer and initialize the memory yourself:
sub esp, 4
mov [esp], '%'
mov [esp + 1], 'd'
mov [esp + 2], '\t'
mov [esp + 3], '\0'
...
push ecx
push esp + 4
call printf
Note this is one way. Not necessarily the best way. The best way being let C do your memory management for you.

How to reverse an array in Irvine Assembly Language?

I am writing a number manipulation program, and one step is to reverse the array. So far I have
reverseArray proc
mov eax, arraySize
mov temp, eax
Imul eax, 4
mov esi, eax
mov eax, 0
mov number, 0
jne L1
L1:
cmp temp, 0
je L3
mov eax, numbers[esi]
mov tempnumbers[ecx], eax
mov eax, tempnumbers[ecx]
call writeDec
call crlf
sub esi,4
add ecx, 4
sub temp, 1
loop L1
L2:
ret
L3:
.If(number >= 0)
mov esi, 0
.ENDIF
mov eax, number
cmp eax, arraySize
je L2
mov eax, tempnumbers[esi]
mov numbers[esi], eax
add esi, 4
add number, 1
loop L3
However this only reverses about half of my array. Did I do something wrong with the esi or ecx counters? Any help would be appreciated.
Use edi instead of ecx, and set it to 0 at the start.

Resources