Link to home
Start Free TrialLog in
Avatar of Steven Wright
Steven Wright

asked on

Transfer A bubble sort from C to 3 Assembly Languages.

Hey there guys.

I have been given a chunk of C++ Code that I need to turn into assembly for 3 Processors.

The, 6805, DS89C420 & ARM7

Below is the code.
Any Help would be amazing.

void BubbleSort (void) {
 unsigned char tmp;
 int i, j;
 for (i=0; i<sizeof(Data)-1; i++) {
 for (j=0; j<sizeof(Data)-1-i; j++) {
 if (Data[j+1] < Data[j]) {
 tmp = Data[j];
 Data[j] = Data[j+1];
 Data[j+1] = tmp;
 }
 }
 }
 }
Avatar of sarabande
sarabande
Flag of Luxembourg image

the last time i made Assembler is nearly 40 years ago ...  but i got some code from jose rodriguez

.model small
.stack 100h
.data

arr dw 7,99,1418,0,521,66,255 ;ARRAY OF 7 INTEGERS.
i   dw ?
j   dw ?

;-----------------------------------------

.code
start:

;INITIALIZE DATA SEGMENT.
  mov  ax, @data
  mov  ds, ax                 

  call bubble_sort_descending

;WAIT FOR ANY KEY.    
  mov  ah, 7
  int  21h

;FINISH PROGRAM.
  mov  ax, 4c00h
  int  21h

;-----------------------------------------
;for ( i = 0; i < len-1; i++ )
;  for ( j = i+1; j < len; j++ )
;    if ( arr[i] < arr[j] ) // '<' BECAUSE IT'S ASCENDING.
;      exchange

bubble_sort_descending proc
  mov  i, 0             ;I = 0.
fori:
  mov  ax, i            ;AX = I.
  inc  ax               ;I++.
  mov  j, ax            ;J = I++.
forj:
;GET ARR[ I ].
  mov  si, offset arr
  mov  ax, i
  shl  ax, 1            ;I * 2, BECAUSE EVERY COUNTER IS 2 BYTES.
  add  si, ax
  mov  ax, [ si ]       ;AX = ARR[ I ].
;GET ARR[ J ].
  mov  di, offset arr
  mov  cx, j
  shl  cx, 1            ;J * 2, BECAUSE EVERY COUNTER IS 2 BYTES.
  add  di, cx
  mov  cx, [ di ]       ;CX = ARR[ J ].
;IF ( ARR[ I ] < ARR[ J ] ).
  cmp  ax, cx           ;CMP ARR[ I ], ARR[ J ].
  jae  bigger           ;IF ( ARR[I] >= ARR[J] ) NO EXCHANGE.
;EXCHANGE BECAUSE ARR[ I ] IS NOT BIGGER THAN ARR[ J ].
  ;EXCHANGE COUNTERS IN ARR.
    mov  [ si ], cx     ;ARR[ I ] = ARR[ J ].
    mov  [ di ], ax     ;ARR[ J ] = ARR[ I ].
bigger:
;NEXT J.
  inc  j                ;J++.
  cmp  j, 7
  jbe  forj             ;IF ( J <= 7 ) REPEAT.  
;NEXT I.
  inc  i                ;I++.
  cmp  i, 7
  jb   fori             ;IF ( I <  7 ) REPEAT.  

  ret
bubble_sort_descending endp    

;-----------------------------------------

end start
share
improve this answer

Open in new window


it is for another processor but should give you the main principles of a conversion from c to assembler.

note, the sample code sorts dword type what is unsigned int. if you want to sort unsigned char you need code to copy from byte to dword and back.

note, most c/c++ compilers have an option to show disassembly. this may contain a flavour of assembly code which could not transferred 1:1 to your micro processors but generally gives you a valuable idea of how the code should look like.

Sara
ASKER CERTIFIED SOLUTION
Avatar of South Mod
South Mod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial